如何将我的 xml 行序列化为 C# 对象

How to serialize my xml rows in to c# objects

我有样品xml

 <ListingDoc version="1.0">
 <Areas>
 <Area Area_Seq="1" Area_Name="Mumbai" Area_Code="MUM"/>
 <Area Area_Seq="1" Area_Name="Delhi" Area_Code="DEL"/>
 </Areas>
<Companies>
<Company Company_Name="ABCD"  Company_Rating="5"  Company_Parent=""/>
<Company Company_Name="XYZ"  Company_Rating="12"  Company_Parent="ABCD"/>
<Company Company_Name="MAN"  Company_Rating="77"  Company_Parent=""/>
</Companies>
</ListingDoc>

并且我想使用 c# 在相应的对象中使用序列化此 xml。但是当我这样做时,只有交替的行出现在对象中。我使用下面写的代码

   XmlDataDocument xmldoc = new XmlDataDocument();
   FileStream xmlFile=null;
   xmlFile = new FileStream("c://temp//Listing.xml", FileMode.Open, FileAccess.Read);
   using (xmlFile)
    {
       XmlNode n1= xmldoc.DocumentElement;
       XmlNodeList nodes = n1.SelectNodes("Companies");
       if (nodes != null && nodes.Count > 0)
       {
           //log session node found
           XmlDataDocument companyXml= new XmlDataDocument();
           companyXml.LoadXml(nodes[0].OuterXml);
           XmlNode Tag_comp = companyXml.DocumentElement;
           XmlReader xmlReader = new XmlNodeReader(Tag_comp);
           List<Company> companyList=new List<Company>();
            using (xmlReader)
            {
               while (xmlReader.Read())
               {
                   if (xmlReader.AttributeCount > 0)
                   {
                       System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(Company));
                       var session = (Company)ser.Deserialize(xmlReader);
                       companyList.Add(session);

                   }
               }
             }
        }
      }

这会用备选行填充我的列表。请建议我修复它,因为我发现当我序列化我的行时 xmlreader 前进到下一条记录并且我也在 while 循环中使用了 xmlReader.Read()。

或者我也尝试使用 XDocument。但它给了我错误 root element is missing 所以给我一些建议。

在我看来你自己做的太多了。让框架全程为您处理 XML 到 Object 的转换。

以下示例使用自定义路径(因此请更改它),但它确实采用了您的 XML 结构。这应该会提供足够的提示让您继续前进。

class Program
{
    const string filename = @".\Example.xml";
    static void Main(string[] args)
    {
        XmlSerializer xSer = new XmlSerializer(typeof(ListingDoc), new Type[] { typeof(Company), typeof(Area) });
        using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
        {
            // load from disk into object model
            ListingDoc listing = xSer.Deserialize(fs) as ListingDoc;

            // output loaded info
            listing.Areas.ForEach(area => Console.WriteLine("Area: {0}, {1}, {2}", area.Name, area.Sequence, area.Code));

            listing.Companies.ForEach(company => Console.WriteLine("Companies: {0}, {1}, {2}", company.Name, company.Rating, company.Parent));

        }
    }
}

public class ListingDoc
{
    public List<Area> Areas;
    public List<Company> Companies;
}

public class Company
{
    [XmlAttribute("Company_Rating")]
    public int Rating;

    [XmlAttribute("Company_Name")]
    public string Name;

    [XmlAttribute("Company_Parent")]
    public string Parent;
}

public class Area {
    [XmlAttribute("Area_Seq")]
    public int Sequence;

    [XmlAttribute("Area_Name")]
    public string Name;

    [XmlAttribute("Area_Code")]
    public string Code;
}