我想要一个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);
答案 0 :(得分:2)
首先,创建ElementData
元素add add the namespace prefix 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")
);