1

我正在努力使用 .NET 中的 XmlDocument 对象输出以下 XML。有什么建议么?

这就是我想要输出的...

<l:config
    xmlns:l="urn:LonminFRConfig"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="urn:LonminFRConfig lonminFRConfigSchema.xsd">

</l:config>

命名空间真的让我很难受!

4

2 回答 2

2

试试这个:

XmlDocument xmlDoc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("l", "urn:LonminFRConfig");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

XmlElement config = xmlDoc.CreateElement("l:config", nsmgr.LookupNamespace("l"));
XmlAttribute schemaLocation = xmlDoc.CreateAttribute(
    "xsi:schemaLocation", nsmgr.LookupNamespace("xsi"));
config.Attributes.Append(schemaLocation);
schemaLocation.Value = "urn:LonminFRConfig lonminFRConfigSchema.xsd";

xmlDoc.AppendChild(config);
xmlDoc.Save(Console.Out);

祝你好运!

于 2009-10-14T14:53:28.727 回答
0

蒂会做到的。

const string lNS = "urn:lLominFRConfig";
const string xsiNS = "http://www.w3.org/2001/XMLSchema-instance";

var dom = new XmlDocument();

var configElem = dom.AppendChild(dom.CreateElement("l:config", lNS));

configElem.Attributes.Append(dom.CreateAttribute("xsi:schemaLocation", xsiNS))
    .Value = "urn:LonminFRConfig lonminFRConfigSchema.xsd";
于 2009-10-14T14:58:07.593 回答