2016-08-01 63 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") 
);