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

c#-从XML文件获取数据树,c#,xml,C#,Xml,我有一个XML文件,它有许多子文件: x xxx xx xxxx xx xx xx xx 名称公司 xx xx xx xx xx xx xx xx xx xx xxL xx xx xx xx xx xx xx xx 1. LSML1710 xx xx xx x x ... 例如,我想做的一件事是获取标记Doc中的数据,然后Anag 目前我有以下课程: 公共类Anag { 公共字符串RagioneSociale{get;set;} 公共字符串Indirizzo{get;set;} 公共字

我有一个XML文件,它有许多子文件:


x
xxx
xx
xxxx xx xx
xx

xx
名称公司
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xxL
xx
xx
xx
xx
xx
xx
xx

xx
1.
LSML1710
xx

xx
xx
x
x
...
例如,我想做的一件事是获取标记
Doc
中的数据,然后
Anag

目前我有以下课程:

公共类Anag
{
公共字符串RagioneSociale{get;set;}
公共字符串Indirizzo{get;set;}
公共字符串CAP{get;set;}
公共字符串Citta{get;set;}
公共字符串Provincia{get;set;}
公共字符串CodiceNazione{get;set;}
公共字符串Telefono{get;set;}
公共字符串{get;set;}
公共字符串电子邮件{get;set;}
}
公共类文件
{
公共字符串DocNumber{get;set;}
公共Anag Anags{get;set;}
}
List results=contentFile.substands(“Doc”).Select(Doc=>new Doc//var results=contentFile.substands(“Doc”).SelectMany(Doc=>Doc.substands(Doc.Name.Namespace+“Anag”).Select(Anag=>new
{
DocNumber=(字符串)doc.Element(doc.Name.Namespace+“DocNumber”),
Anags=doc.substands(doc.Name.Namespace+“Anag”)。选择(Anag=>newanag
{
RagioneSociale=(字符串)anag.Element(anag.Name.Namespace+“Name”),
Indirizzo=(字符串)anag.Element(anag.Name.Namespace+“Address”),
CAP=(字符串)anag.Element(anag.Name.Namespace+“ZipCode”),
Provincia=(字符串)anag.Element(anag.Name.Namespace+“省”),
Telefono=(字符串)anag.Element(anag.Name.Namespace+“PhoneNumber”),
Cellulare=(字符串)anag.Element(anag.Name.Namespace+“CellularNumber”),
Email=(字符串)anag.Element(anag.Name.Namespace+“EmailAddress”),
Citta=(字符串)anag.Element(anag.Name.Namespace+“City”)
}),
}).ToList();
最后一个代码给出了一个错误:

无法在
中隐式转换类型
system.collections.generic.IEnumerable

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

名单:

+Doc=>
{DocNumber}
+Anag=>{地址、姓名……(不是列表!)
+DestinationAddress=>{地址….名称..)

因此,我只能转换为.csv文件。

只需尝试使用“特殊粘贴”功能生成用于解析的类。您将100%不会得到异常

  • Crtl+A在整个xml上,复制到剪贴板
  • 然后创建一个新类 在项目中单击“编辑”->“粘贴特殊”->“将XML粘贴为” “类”
  • 将解析更改为使用 粘贴类
  • 构建并尝试它

  • 既然您已经经历了创建类的麻烦,为什么不让反序列化程序为您完成这项工作呢

    以下是方法:

    XmlSerializer serializer = new XmlSerializer(typeof(Root));
    using (var reader = new StreamReader(xmlFilePath))
    {
        List<Doc> docs = ((Root)serializer.Deserialize(reader)).Docs;
    }
    
    XmlSerializer serializer=新的XmlSerializer(typeof(Root));
    使用(var reader=newstreamreader(xmlFilePath))
    {
    列表文档=((根)序列化程序.反序列化(读取器)).docs;
    }
    
    现在,您只需要在类属性中添加一些标记,以便它们与xml名称匹配

    [Serializable, XmlRoot("Docs")]
    public class Root
    {
        [XmlElement("Doc")]
        public List<Doc> Docs { get; set; }
    }
    
    public class Doc
    {
        public string DocNumber { get; set; }
    
        [XmlElement("Anag")]
        public Anag Anags { get; set; }
    }
    
    public class Anag
    {
        [XmlElement("Name")]
        public string RagioneSociale { get; set; }
    
        [XmlElement("Address")]
        public string Indirizzo { get; set; }
    
        [XmlElement("ZipCode")]
        public string CAP { get; set; }
    
        [XmlElement("City")]
        public string Citta { get; set; }
    
        [XmlElement("Province")]
        public string Provincia { get; set; }
    
        [XmlElement("CountryCode")]
        public string CodiceNazione { get; set; }
    
        [XmlElement("PhoneNumber")]
        public string Telefono { get; set; }
    
        [XmlElement("CellularNumber")]
        public string Cellulare { get; set; }
    
        [XmlElement("EmailAddress")]
        public string Email { get; set; }
    }
    
    [可序列化,XmlRoot(“文档”)]
    公共类根
    {
    [XmlElement(“Doc”)]
    公共列表文档{get;set;}
    }
    公共类文件
    {
    公共字符串DocNumber{get;set;}
    [XmlElement(“Anag”)]
    公共Anag Anags{get;set;}
    }
    公开课
    {
    [XmlElement(“名称”)]
    公共字符串RagioneSociale{get;set;}
    [XmlElement(“地址”)]
    公共字符串Indirizzo{get;set;}
    [XmlElement(“ZipCode”)]
    公共字符串CAP{get;set;}
    [XmlElement(“城市”)]
    公共字符串Citta{get;set;}
    [XmlElement(“省”)]
    公共字符串Provincia{get;set;}
    [XmlElement(“国家代码”)]
    公共字符串CodiceNazione{get;set;}
    [XmlElement(“电话号码”)]
    公共字符串Telefono{get;set;}
    [XmlElement(“CellularNumber”)]
    公共字符串{get;set;}
    [XmlElement(“电子邮件地址”)]
    公共字符串电子邮件{get;set;}
    }
    
    “因此我只能转换为.csv文件”改为使用。^--您可以,但转换本身将在转换文件中定义,使用专门为该任务设计的语言。这将更加方便,让您可以自由更改(调整或修复)它,而无需重新编译C代码。(不是一个答案,因为我建议使用完全不同的东西。)在这里得到启发:我正在使用一个WPF项目,而不是.NET,我认为它与.NETFramework一起工作。什么意思?Net?“WINDOWS演示基金会(WPF)是一个图形子系统(类似于WINFALSE)。最初由Microsoft开发,用于在基于Windows的应用程序.WPF(以前称为“Avalon”)中呈现用户界面“,最初是在2006年作为.NET Framework 3.0的一部分发布的。WPF使用DirectX并试图为构建应用程序提供一致的编程模型。它将用户界面与业务逻辑分离,并类似于类似的面向XML的对象模型,例如在XUL和SVG中实现的对象模型。“好吧,我不知道是我干的,课程是通用电气的