c# - 如何使用子箭头创建XML

时间:2016-11-22 13:42:03

标签: c# xml xmlserializer

我是c#和XML的新手。 我有xsd文件并从中创建了cs文件,如此处所述Generating XML file using XSD file 现在我需要生成包含多张发票的XML文件,每张发票都有一个或多个子目录。 怎么做?

1 个答案:

答案 0 :(得分:0)

1启动VS命令提示符

2生成类,基于带有SvcUtil.exe的XSD

svcutil xml_in_arved.xsd /language:C# /dataContractOnly /importxmltypes   /out:class.cs

3编写代码来填充类

XmlDocument doc = new XmlDocument();
doc.LoadXml("<book>" +
                    "  <title>Oberon's Legacy</title>" +
                    "  <price>5.95</price>" +
                    "</book>");

invoices invoices = new invoices();
invoices.Nodes = new XmlNode[2];
invoices.Nodes[0] = doc.CreateNode("element", "test", "myNamespace");
invoices.Nodes[1] = doc.CreateNode("element", "tes2", "myNamespace");

4将类序列化为xml

序列化示例

var content = invoices;
var filename = @"c:\temp\xmlserialise.xml";
using (TextWriter textWriter = new StreamWriter(filename, false))
{
    var serializer = new XmlSerializer(content.GetType());
    serializer.Serialize(textWriter, content);
}