如何从xml文档中创建简单类?

时间:2016-04-12 10:08:47

标签: c# xml-parsing xsd.exe

xml片段:

...
<invoiceID value=""/> 
<invUNP value=""/> 
<invDateCreate value=""/>
...

使用xsd获取下一个代码

[System.Xml.Serialization.XmlElementAttribute("invoiceID",Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public getInvoiceReplyInvoiceID[] invoiceID;
public partial class getInvoiceReplyInvoiceID {
  [System.Xml.Serialization.XmlAttributeAttribute()]
   public string value;
}

这段代码不简单

使用http://xmltocsharp.azurewebsites.net/获取c#代码

...
[XmlRoot(ElementName = "invoiceID")]
public class InvoiceID
{
    [XmlAttribute(AttributeName = "value")]
    public string Value { get; set; }
}

[XmlRoot(ElementName = "invUNP")]
public class InvUNP
{
    [XmlAttribute(AttributeName = "value")]
    public string Value { get; set; }
}
[XmlElement(ElementName = "invoiceID")]
public InvoiceID InvoiceID { get; set; }
[XmlElement(ElementName = "invUNP")]
public InvUNP InvUNP { get; set; }

...

如何获得下一个c#类?

...
[XmlElement(ElementName = "invoiceID")]
public string InvoiceID { get; set; }
[XmlElement(ElementName = "invUNP")]
public string InvUNP { get; set; }
...

可能使用svcutil.exe? 或写自解析器?

1 个答案:

答案 0 :(得分:0)

你需要的就是这里=&gt; MSDN

首先将xml解析为xsd,将xsd解析为.cs