c#-从XML文件获取数据树

问题描述 投票:0回答:1

我有一个包含许多子项的XML文件:

<Docs>
<Doc>
<DocType>x</DocType>
<DocNumber>xxx</DocNumber>
<DocNumberSeries>xx</DocNumberSeries>
<DocDate>xxxx-xx-xx</DocDate>
<DocCause>
<Id>xx</Id>
<Code/>
<Name>xx</Name>
</DocCause>
<Anag>
<Name>NameCompany</Name>
<Address>xx</Address>
<ZipCode>xx</ZipCode>
<City>xx</City>
<Province>xx</Province>
<CountryCode>xx</CountryCode>
<PhoneNumber>xx</PhoneNumber>
<CellularNumber/>
<FaxNumber>xx</FaxNumber>
<EmailAddress>xx</EmailAddress>
<VatNumber>xx</VatNumber>
<PersonalID>xx</PersonalID>
</Anag>
<DestinationAddress>
<Name>xxL</Name>
<Address>xx</Address>
<ZipCode>xx</ZipCode>
<City>xx</City>
<Province>xx</Province>
<CountryCode>xx</CountryCode>
<PhoneNumber>xx</PhoneNumber>
<FaxNumber>xx</FaxNumber>
</DestinationAddress>
<Payment>
<Code/>
<Name>xx</Name>
</Payment>
<DocRows>
<DocRow>
<RowType>1</RowType>
<Product>
<Code>LSML1710</Code>
</Product>
<Description></Description>
<Quantity></Quantity>
<Price></Price>
<Tax>
<Tax>
<Id>xx</Id>
<Code/>
<Name>xx</Name>
<PercentAmount>xx</PercentAmount>
<IndPercentAmount>x</IndPercentAmount>
<FixedAmount>x</FixedAmount>
</Tax>
</Tax>
</DocRow>
</DocRows>
..
</Doc>

-

例如,我想做的一件事就是在标签Doc中获取数据,然后在Anag中获取。

目前我有这些课程:

public class Anag
    {
        public string RagioneSociale { get; set; }
        public string Indirizzo { get; set; }
        public string CAP { get; set; }
        public string Citta { get; set; }
        public string Provincia { get; set; }
        public string CodiceNazione { get; set; }
        public string Telefono { get; set; }
        public string Cellulare { get; set; }
        public string Email { get; set; }
    }

    public class Doc
    {
        public string DocNumber { get; set; }
        public Anag Anags { get; set; }
    }

                     List<Doc> results = contentFile.Descendants("Doc").Select(doc => new Doc                 // var results = contentFile.Descendants("Doc").SelectMany(doc => doc.Descendants(doc.Name.Namespace + "Anag").Select(anag => new
                    {
                        DocNumber = (string)doc.Element(doc.Name.Namespace + "DocNumber"),
                        Anags = doc.Descendants(doc.Name.Namespace + "Anag").Select(anag => new Anag
                        {
                            RagioneSociale = (string)anag.Element(anag.Name.Namespace + "Name"),
                            Indirizzo = (string)anag.Element(anag.Name.Namespace + "Address"),
                            CAP = (string)anag.Element(anag.Name.Namespace + "ZipCode"),
                            Provincia = (string)anag.Element(anag.Name.Namespace + "Province"),
                            Telefono = (string)anag.Element(anag.Name.Namespace + "PhoneNumber"),
                            Cellulare = (string)anag.Element(anag.Name.Namespace + "CellularNumber"),
                            Email = (string)anag.Element(anag.Name.Namespace + "EmailAddress"),
                            Citta = (string)anag.Element(anag.Name.Namespace + "City")
                        }),
                    }).ToList();

此最后一个代码给出错误:

无法隐式转换类型为system.collections.generic.IEnumerable的类型。

我想要的最终结果是一个包含此Xml的数据和子元素的列表:

列表:

  • Doc =>{DocNumber}
  • Anag => {地址..,名称..。)(不是列表!)
  • DestinationAddress => {地址..名称..]

以便仅可以转换为.csv文件

c#
1个回答
0
投票
只需尝试使用“特殊粘贴”功能来生成要解析的类。您将100%不例外。

推荐问答