xml中的命名空间问题

时间:2012-05-23 15:08:58

标签: c# asp.net xml xml-serialization xsd

XmlElement xmlElementSAPD = xmlDocument.CreateElement("SAPD");
root.AppendChild(xmlElementSAPD);

xmlElementSAPD.AppendChild(XmlFunctions.GetXMLElement(xmlDocument, "smu", dr.GetString("sma").Trim()));

上面的c#代码在下面创建了XML。

<SAPD>
<smu>123</smu>
</SAPD>

如何更改上面的代码以便我可以

<ns0:SAPD>
<ns0:smu>123</ns0:smu>
</ns0:SAPD>

任何?如何在xml节点中添加ns0:

2 个答案:

答案 0 :(得分:0)

您必须将命名空间添加到xml文档中。

只是为了给你一个想法,看看下面的

XmlDocument doc = new XmlDocument();  
XmlSchema schema = new XmlSchema();
schema.Namespaces.Add("ns0", "http://www.sample.com/file");
doc.Schemas.Add(schema);

答案 1 :(得分:0)

前缀ns0需要与命名空间映射,否则xml无效。

假设ns0映射到http://tempuri.org,可以使用以下代码:

        XmlDocument doc = new XmlDocument();
        XmlElement sapd= doc.CreateElement("ns0","SAPD","http://tempuri.org");
        XmlElement smu = doc.CreateElement("ns0", "smu", "http://tempuri.org");
        smu.InnerText = "123";
        sapd.AppendChild(smu);
        doc.AppendChild(sapd);
        doc.InnerXml.ToString();
相关问题