可當成讀取設定檔的方式使用
範例檔:https://msdn.microsoft.com/zh-tw/library/bb669158.aspx
教學說明:https://msdn.microsoft.com/zh-tw/library/bb387041.aspx
static void ReadFromXML()
{
try
{
XElement root = XElement.Load("../../App_Data/XML01.xml");
XNamespace aw = "http://www.adventure-works.com";
Console.WriteLine(root.Attribute(aw + "PurchaseOrderNumber").Value);
Console.WriteLine(root.Attribute(aw + "OrderDate").Value);
IEnumerable<XElement> address =
from el in root.Elements(aw + "Address")
where (string)el.Attribute(aw + "Type") == "Billing"
select el;
foreach (XElement el in address)
Console.WriteLine(el);
foreach (XElement el in address)
Console.WriteLine(el.Element(aw+"Zip").Value);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
輸出結果:
99503
1999-10-20
<aw:Address aw:Type="Billing" xmlns:aw="http://www.adventure-works.com">
<aw:Name>Tai Yee</aw:Name>
<aw:Street>8 Oak Avenue</aw:Street>
<aw:City>Old Town</aw:City>
<aw:State>PA</aw:State>
<aw:Zip>95819</aw:Zip>
<aw:Country>USA</aw:Country>
</aw:Address>
95819