当前位置:编程学习 > C#/ASP.NET >>

c#生成xml文件, 怎么写一个带namespace的节点?

<opentrans:OPENTRANS xmlns:opentrans="http://www.opentrans.org/XMLSchema/2.1fd">
<opentrans:ORDER version="2.1fd" type="standard"/>
</opentrans:OPENTRANS>
        

要生成上面的XML,用c#要怎么写?

求帮忙。 --------------------编程问答--------------------

XmlDocument.CreateElement("opentrans:OPENTRANS");
这样子不行吗? "opentrans:OPENTRANS"这个只会当做一个字符串来处理吧
--------------------编程问答--------------------
            XmlDocument xml = new XmlDocument();
            XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "utf-8", null);
            xml.AppendChild(dec);
            XmlElement root = xml.CreateElement("opentrans", "OPENTRANS", "http://www.opentrans.org/XMLSchema/2.1fd");
            xml.AppendChild(root);
            XmlElement element = xml.CreateElement("opentrans:ORDER", "http://www.opentrans.org/XMLSchema/2.1fd");
            element.SetAttribute("version", "2.1fd");
            element.SetAttribute("type", "standard");
            root.AppendChild(element);
            xml.Save(@"E:\1.xml");
<?xml version="1.0" encoding="utf-8"?>
<opentrans:OPENTRANS xmlns:opentrans="http://www.opentrans.org/XMLSchema/2.1fd">
  <opentrans:ORDER version="2.1fd" type="standard" />
</opentrans:OPENTRANS>
--------------------编程问答--------------------
引用 2 楼  的回复:
C# code

            XmlDocument xml = new XmlDocument();
            XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "utf-8", null);
            xml.AppendChild(dec);
            XmlElement……


++  

其实直接拼就OK了。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,