////检索属性type的值
XElement val = new XElement("Value",
new XAttribute("ID", "1243"),
new XAttribute("Type", "int"),
new XAttribute("ConvertableTo", "double"),
"100");
IEnumerable<XAttribute> xa =
from att in val.Attributes()
select att;
foreach (XAttribute a in xa)
if (a.Name.ToString()== "Type")
Console.WriteLine(a.Value);
XElement cust = new XElement("PhoneNumbers",
new XElement("Phone",
new XAttribute("type", "home"),
"555-555-5555"),
new XElement("Phone",
new XAttribute("type", "work"),
"555-555-6666")
);
IEnumerable<XElement> elList =
from el in cust.Descendants("Phone")
select el;
foreach (XElement el in elList)
Console.WriteLine((string)el.Attribute("type"));
////查找具有特定属性的元素
XElement root = XElement.Load("PurchaseOrder.xml");
IEnumerable<XElement> address =
from el in root.Elements("Address")
where (string)el.Attribute("Type") == "Billing"
select el;
foreach (XElement el in address)
Console.WriteLine(el);
////查找具有特定子元素的元素
XElement root = XElement.Load("TestConfig.xml");
IEnumerable<XElement> tests =
from el in root.Elements("Test")
where (string)el.Element("CommandLine") == "Examp2.EXE"
select el;
foreach (XElement el in tests)
Console.WriteLine((string)el.Attribute("TestId"));
浙公网安备 33010602011771号