LinqToXml簡介2:加入Namespace命名空間
在真實的世界裡面,XML往往會被加入一大堆Namespace命名空間來進行特性的描述,這些名稱空間一旦用上LINQ to XML後,語法就沒有那麼的簡單了。
靜態方式加入Namespace(XmlNS)
請直接看程式碼,語法如下:
System.Xml.Linq.XDocument oXml = new System.Xml.Linq.XDocument(
new System.Xml.Linq.XDeclaration("1.0", "UTF-8", "yes"),
new System.Xml.Linq.XElement("Employees",
new System.Xml.Linq.XAttribute(System.Xml.Linq.XNamespace.Xmlns + "AAA", "urn:SomeDescribe:xmlns:slashviewA:1.0"),
new System.Xml.Linq.XAttribute(System.Xml.Linq.XNamespace.Xmlns + "BBB", "http://www.w3.org/"),
new System.Xml.Linq.XAttribute(System.Xml.Linq.XNamespace.Xmlns + "CCC", "SomethiongICanNotTellYou"),
new System.Xml.Linq.XElement("Person",
new System.Xml.Linq.XAttribute("ID", "A1234"),
new System.Xml.Linq.XElement("Name", "王小明"),
new System.Xml.Linq.XElement("Address", "台北市OOO路")
),
new System.Xml.Linq.XElement("Person",
new System.Xml.Linq.XAttribute("ID", "A5678"),
new System.Xml.Linq.XElement("Name", "李小華"),
new System.Xml.Linq.XElement("Address", "高雄市OOO路")
)
)
);
輸出的XML已經有看到諸多的XML Namespace被加入了,例如:AAA、BBB...
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Employees xmlns:AAA="urn:SomeDescribe:xmlns:slashviewA:1.0" xmlns:BBB="http://www.w3.org/" xmlns:CCC="SomethiongICanNotTellYou">
<Person ID="A1234">
<Name>王小明</Name>
<Address>台北市OOO路</Address>
</Person>
<Person ID="A5678">
<Name>李小華</Name>
<Address>高雄市OOO路</Address>
</Person>
</Employees>
動態的方式加入Namespace(XmlNS)
有時候我們不可能只會在根元素裡面添加XML Namespace而已,有很多情況是子元素也要添加Namesapce,甚至XML的屬性(Attributes)也逃不了。因此下列程式來示範LINQ to XML如何進行動態元素之名稱空間添加。
System.Xml.Linq.XNamespace oXmlNsAAA = "這完全是我亂打的一個字串";
System.Xml.Linq.XNamespace oXmlNsBBB = "http://www.foo.com/";
System.Xml.Linq.XDocument oXml = new System.Xml.Linq.XDocument(
new System.Xml.Linq.XDeclaration("1.0", "UTF-8", "yes"),
new System.Xml.Linq.XElement(oXmlNsAAA + "Employees",
new System.Xml.Linq.XAttribute(System.Xml.Linq.XNamespace.Xmlns + "AAA", "這完全是我亂打的一個字串"),
new System.Xml.Linq.XAttribute(System.Xml.Linq.XNamespace.Xmlns + "BBB", "http://www.foo.com/"),
new System.Xml.Linq.XElement(oXmlNsAAA + "Person",
new System.Xml.Linq.XAttribute(oXmlNsBBB + "ID", "A1234"),
new System.Xml.Linq.XElement(oXmlNsAAA + "Name", "王小明"),
new System.Xml.Linq.XElement(oXmlNsAAA + "Address", "台北市OOO路")
),
new System.Xml.Linq.XElement(oXmlNsAAA + "Person",
new System.Xml.Linq.XAttribute(oXmlNsBBB + "ID", "A5678"),
new System.Xml.Linq.XElement(oXmlNsAAA + "Name", "李小華"),
new System.Xml.Linq.XElement(oXmlNsAAA + "Address", "高雄市OOO路")
)
)
);
XNamespace使用要點如下:
- 利用靜態的方式,宣告一個Xml Namespace,且給定一個獨一無二的字串(Key)。
- 利用XNamespace類別以變數的方式動態宣告,並給定在XDocument靜態宣告的一致性獨一無二的字串(Key)。若不一致會造成XDocument識別錯誤,認為這是兩個不一樣的Namespace,進而幫你進行補充添加,出來的結果一定不會如你所想像(會有垃圾資料)。
- 使用XNamespace與+這個操作子,進行XElement類別的命名空間添加。
- XDocument有很強的命名空間整理功能,你利用動態方法使用到到沒有宣告過的命名空間,在輸出時期他會幫你利用xmlns補宣告。設計時期重複宣告引入宣告過的命名空間,在輸出時期他會幫你去除重複。
最後產生出來的XML文件:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<AAA:Employees xmlns:AAA="這完全是我亂打的一個字串" xmlns:BBB="http://www.foo.com/">
<AAA:Person BBB:ID="A1234">
<AAA:Name>王小明</AAA:Name>
<AAA:Address>台北市OOO路</AAA:Address>
</AAA:Person>
<AAA:Person BBB:ID="A5678">
<AAA:Name>李小華</AAA:Name>
<AAA:Address>高雄市OOO路</AAA:Address>
</AAA:Person>
</AAA:Employees>