2016-08-01 59 views
0

XML節點我想要的XML輸出作爲下如何創建與結腸癌

<ExtendedData xmlns:section="http://svr:1245/contact/kml/section.xsd"> 
    <section:secid>svr_01</section:secid> 
    <section:name>test</unit:name> 
    </ExtendedData> 

我怎樣才能做到這一點?我的代碼如下,但輸出不正確

var attribute = xDoc.CreateAttribute("section","secid","http://svr:1245/contact/kml/section.xsd"); 

XmlElement elementExtendedData = xDoc.CreateElement("ExtendedData"); 
elementPlacemark.AppendChild(elementExtendedData); 

var elementSectionid = xDoc.CreateElement("section", "secid"); 
attribute.InnerText = UniqueID; 
elementSectionid.Attributes.Append(attribute); 
elementExtendedData.AppendChild(elementSectionid); 

回答

1

首先,創建ElementData元素添加添加命名空間前綴xmlns:section。然後添加具有正確前綴和名稱空間的元素。

var extendedData = xDoc.CreateElement("ExtendedData"); 
extendedData.SetAttribute("xmlns:section", "http://svr:1245/contact/kml/section.xsd"); 

elementPlacemark.AppendChild(extendedData); 

var secId = xDoc.CreateElement("section", "secid", "http://svr:1245/contact/kml/section.xsd"); 
secId.InnerText = "svr_01"; 

extendedData.AppendChild(secId); 

如果可以選擇,我會建議使用LINQ to XML相反,它是好得多一起工作:

XNamespace ns = "http://svr:1245/contact/kml/section.xsd"; 

var element = new XElement("ExtendedData", 
    new XAttribute(XNamespace.Xmlns + "section", ns), 
    new XElement(ns + "secid", "svr_01") 
);