C# 创建XML文档
有些时候我们需要生成一个xml文档作为数据交换的容器。当然我们用拼接字符串的方法来进行构建xml,但是这种方法虽然简单有效,但是如果xml文档结构过于复杂,拼接字符串会让人眼花缭乱。这时候就需要C#给我们提供现成的类库,以供我们自由的创建xml文档。
比如我们要创建如下的xml文档
<?xml version="1.0" encoding="UTF-8"?> <ns0:Z_AVS_UPLOAD_WEIGHT_Request xmlns:ns0="http://schemas.microsoft.com/BizTalk/2003"> <ZWERKS>ZFM1</ZWERKS> <TAB1> <ZNO>13022101</ZNO> <ZORDER>2013238955</ZORDER> <ZWEIGHT>4140</ZWEIGHT> </TAB1> <TAB1> <ZNO>13022101</ZNO> <ZORDER>2013239627</ZORDER> <ZWEIGHT>4140</ZWEIGHT> </TAB1> </ns0:Z_AVS_UPLOAD_WEIGHT_Request>
选取这样的结构,一方面是因为它来自于论坛某位坛友实际的需求,另一方面它足够简单却有代表性。
下面我将以这个例子用两种方法(XmlDocument和Linq to XML)进行讲解。
1、XmlDocument
XmlDocument document = new XmlDocument(); XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", "UTF-8", "");//xml文档的声明部分 document.AppendChild(declaration); XmlElement root = document.CreateElement("ns0", "Z_AVS_UPLOAD_WEIGHT_Request", "http://schemas.microsoft.com/BizTalk/2003"); document.AppendChild(root); XmlElement zwerks = document.CreateElement("ZWERKS"); zwerks.InnerText = "ZFM1"; root.AppendChild(zwerks); XmlElement tab1 = document.CreateElement("TAB1"); root.AppendChild(tab1); XmlElement zno = document.CreateElement("ZNO"); zno.InnerText = "13022101"; tab1.AppendChild(zno); XmlElement zorder = document.CreateElement("ZORDER"); zorder.InnerText = "2013238955"; tab1.AppendChild(zorder); XmlElement zweight = document.CreateElement("ZWEIGHT"); zweight.InnerText = "4140"; tab1.AppendChild(zweight); XmlElement tab2 = document.CreateElement("TAB1"); root.AppendChild(tab2); XmlElement zno2 = document.CreateElement("ZNO"); zno2.InnerText = "13022101"; tab2.AppendChild(zno2); XmlElement zorder2 = document.CreateElement("ZORDER"); zorder2.InnerText = "2013238955"; tab2.AppendChild(zorder2); XmlElement zweight2 = document.CreateElement("ZWEIGHT"); zweight2.InnerText = "4140"; tab2.AppendChild(zweight2); document.Save("test.xml");//将生成好的xml保存到test.xml文件中
2、Linq to XML
XDocument document = new XDocument(); document.Declaration = new XDeclaration("1.0", "UTF-8", ""); XNamespace ns = "http://schemas.microsoft.com/BizTalk/2003"; XElement root = new XElement(ns + "Z_AVS_UPLOAD_WEIGHT_Request", new XAttribute(XNamespace.Xmlns + "ns0", "http://schemas.microsoft.com/BizTalk/2003")); root.Add(new XElement("ZWERKS", "ZFM1"), new XElement("TAB1", new XElement("ZNO", 13022101), new XElement("ZORDER", 2013238955), new XElement("ZWEIGHT", 4140)), new XElement("TAB1", new XElement("ZNO", 13022101), new XElement("ZORDER", 2013238955), new XElement("ZWEIGHT", 4140)) ); document.Add(root); document.Save("test.xml");//保存xml到文件
可以发现Linq to XML的方法比较简洁,代码量也足够小。当然了XmlDocument的方法可以进一步的简化,这里重点展示一下Linq to XML的魅力。如上只当给需要的朋友提供一下思路和解决方案。关于Linq to xml的更多信息可以参考:XNamespace。
再提供一个例子:
有a.xml:
<a> <xingming> <item> <name>张三</name> <code>1</code> </item> <item> <name>李四</name> <code>2</code> </item> <item> <name>王五</name> <code>3</code> </item> <item> <name>赵四</name> <code>4</code> </item> <item> <name>王二麻子</name> <code>5</code> </item> </xingming> <nianji> <item> <name>一年级</name> <banji>一班</banji> </item> </nianji> </a>
想转换成b.xml:
<b> <xingming> <items> <item> <Xingname1>张三</Xingname1> <Xingcode1>1</Xingcode1> <Xingname2>李四</Xingname2> <Xingcode2>2</Xingcode2> </item> <item> <Xingname1>王五</Xingname1> <Xingcode1>3</Xingcode1> <Xingname2>赵四</Xingname2> <Xingcode2>4</Xingcode2> </item> <item> <Xingname1>王二麻子</Xingname1> <Xingcode1>5</Xingcode1> </item> </items> </xingming> <nianji> <item> <name>一年级</name> <name>一班</name> </item> </nianji> </b>
代码如下:
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string xml = @"<a> <xingming> <item> <name>张三</name> <code>1</code> </item> <item> <name>李四</name> <code>2</code> </item> <item> <name>王五</name> <code>3</code> </item> <item> <name>赵四</name> <code>4</code> </item> <item> <name>王二麻子</name> <code>5</code> </item> </xingming> <nianji> <item> <name>一年级</name> <banji>一班</banji> </item> </nianji> </a>"; XmlDocument newXmlDoc = new XmlDocument(); XmlElement b = newXmlDoc.CreateElement("b"); newXmlDoc.AppendChild(b); XmlElement xingming = newXmlDoc.CreateElement("xingming"); b.AppendChild(xingming); XmlElement items = newXmlDoc.CreateElement("items"); xingming.AppendChild(items); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlElement item = null; //直接定位到/a/xingming节点下的子节点 var nodes = xmlDoc.SelectSingleNode("/a/xingming").ChildNodes; for (int i = 0; i < nodes.Count; i++) { XmlNode node = nodes.Item(i); //节点项为偶数时,创建item节点 if (i % 2 == 0) { item = newXmlDoc.CreateElement("item"); items.AppendChild(item); } //item节点追加Xingname节点 XmlElement Xingname = newXmlDoc.CreateElement("Xingname" + (i % 2 + 1)); Xingname.InnerText = node.SelectSingleNode("name").InnerText; item.AppendChild(Xingname); //item节点追加Xingcode节点 XmlElement Xingcode = newXmlDoc.CreateElement("Xingcode" + (i % 2 + 1)); Xingcode.InnerText = node.SelectSingleNode("code").InnerText; item.AppendChild(Xingcode); } XmlElement nianji = newXmlDoc.CreateElement("nianji"); b.AppendChild(nianji); XmlElement item1 = newXmlDoc.CreateElement("item"); nianji.AppendChild(item1); XmlElement name = newXmlDoc.CreateElement("name"); name.InnerText = xmlDoc.SelectSingleNode("/a/nianji/item/name").InnerText; item1.AppendChild(name); XmlElement banji = newXmlDoc.CreateElement("name"); banji.InnerText = xmlDoc.SelectSingleNode("/a/nianji/item/banji").InnerText; item1.AppendChild(banji); newXmlDoc.Save("b.xml"); } } }
再附一个例子,生成指定格式的xml文档:
<?xml version="1.0" encoding="utf-8"?> <response> <code>0</code> <date_list> <godate>2017-08-09</godate> <godate>2017-08-19</godate> </date_list> </response>
代码如下:
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { response r = new response() { code = 0, date_list = new date_list() { godate = new string[] { "2017-08-09", "2017-08-19" } } }; XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; settings.Indent = true; XmlWriter writer = XmlWriter.Create("test.xml", settings); XmlSerializer s = new XmlSerializer(typeof(response)); s.Serialize(writer, r, ns); writer.Close(); } } [Serializable] public class response { public int code { get; set; } public date_list date_list { get; set; } } [Serializable] public class date_list { [XmlElement("godate")] public string[] godate { get; set; } } }