在C#中反序列化带有子元素的xml文件

时间:2019-01-22 17:27:50

标签: c# xml xml-deserialization

我正在尝试反序列化此类型的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<Network>
 <ROUTES>
  <ROUTE ID="RT_BALA_GLNC_R_162_154_1" DIRECTION="LEFT" ZONE="Richmond_Hill">
     <ENTRANCESIGNAL>BALA_GLNC_G162</ENTRANCESIGNAL>
     <EXITSIGNAL>BALA_DONS_G154</EXITSIGNAL>
     <POINTENDIDS>
        <POINTENDID POS="N">PT_BALA_GLNC_W11.TrackPortionConnection</POINTENDID>
        <POINTENDID POS="N">PT_BALA_GLNC_W23.TrackPortionConnection</POINTENDID>
     </POINTENDIDS>
  </ROUTE>
  <ROUTE ID="RT_BALA_ORLS_R_111_119_1" DIRECTION="RIGHT" ZONE="Richmond_Hill">
     <ENTRANCESIGNAL>BALA_ORLS_G111</ENTRANCESIGNAL>
     <EXITSIGNAL>BALA_ORLN_G119</EXITSIGNAL>
     <POINTENDIDS>
        <POINTENDID POS="N">PT_BALA_ORLS_W1.TrackPortionConnection</POINTENDID>
     </POINTENDIDS>
  </ROUTE>
  <ROUTE ID="RT_BALA_GLNC_R_162D_154_1" DIRECTION="LEFT" ZONE="Richmond_Hill">
     <ENTRANCESIGNAL>BALA_GLNC_G162D</ENTRANCESIGNAL>
     <EXITSIGNAL>BALA_DONS_G154</EXITSIGNAL>
     <POINTENDIDS>
        <POINTENDID POS="R">PT_BALA_GLNC_W11.TrackPortionConnection</POINTENDID>
        <POINTENDID POS="N">PT_BALA_GLNC_W23.TrackPortionConnection</POINTENDID>
     </POINTENDIDS>
  </ROUTE>
 </ROUTES>
</Network>

我已经尝试过

class Program
{
    static void Main(string[] args)
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(Network));
        TextReader reader = new StreamReader(@"xml File Location");
        object obj = deserializer.Deserialize(reader);
        Network XmlData = (Network)obj;
        reader.Close();
        Console.ReadLine();
    }
}
[XmlRoot("Network")]
public class Network
{
    [XmlElement("ROUTES")]
    public List<ROUTE> ROUTES { get; set; }
}

public class ROUTE
{
    [XmlAttribute("ID")]
    public string ID { get; set; }
    [XmlAttribute("DIRECTION")]
    public string DIRECTION { get; set; }
    [XmlElement("ENTRANCESIGNAL")]
    public string ENTRANCESIGNAL { get; set; }
    [XmlElement("EXITSIGNAL")]
    public string EXITSIGNAL { get; set; }
    [XmlElement("POINTENDIDS")]
    public POINTENDIDS POINTENDIDS { get; set; }
}

public class POINTENDIDS
{
    [XmlElement("POINTENDID")]
    public List<POINTENDID> POINTENDID { get; set; }
}

public class POINTENDID
{
    [XmlAttribute("POS")]
    public string POS { get; set; }
}

我正在控制台应用程序中完成

我开始调试并将断点放在Network XmlData =(Network)obj;

我只有1条路线,并且“ ID”,“ DIRECTION”,“ ENTRANCESIGNAL” ... etc的值设置为Null

作为C#编程的初学者,我不太了解该怎么做!

此实施需要帮助

3 个答案:

答案 0 :(得分:0)

using System.Xml; //XmlDoc
using System.Xml.Linq;//XElement
using System.IO;//Path,File,Directory, Stream

读取并解析xml文件:

XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(XmlFilePath);

另一种方法是使用XElement

XElement a = XElement.Load(@"c:\path\file");

与XmlDocument相比,我通常更喜欢XElement,但这是个人的

如果您从C#开始,则需要一本书和一个更简单的项目。 Streams和Xml在语法上很复杂。此外,控制台应用程序很丑陋,而Forms应用程序与VisualStudio的图形工具并没有那么难。

答案 1 :(得分:0)

修复您的网络课程。方括号中的名称区分大小写。您还需要添加Xml数组属性。

    [XmlRoot("Network")]
    public class Network
    {
        [XmlArrayItem("ROUTE")]
        [XmlArray("ROUTES")]
        public List<ROUTE> ROUTES { get; set; }
    }

答案 2 :(得分:0)

您的C#类与XML文件未完全对齐,并且序列化程序仅返回部分结果。此处概述了如果XML结构是固定的,您可以做些什么。

https://stackoverflow.com/a/17315863/99804

这将按您希望的方式工作。

您将获得以下自动生成的代码。 注意:我已经清理了输出以使用自动属性等。

using System;
using System.Xml.Serialization;

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks />
[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class Network
{
    /// <remarks />
    [XmlArrayItem("ROUTE", IsNullable = false)]
    public NetworkROUTE[] ROUTES { get; set; }
}

[Serializable]
[XmlType(AnonymousType = true)]
public class NetworkROUTE
{
    [XmlAttribute]
    public string DIRECTION { get; set; }

    public string ENTRANCESIGNAL { get; set; }

    public string EXITSIGNAL { get; set; }

    [XmlAttribute]
    public string ID { get; set; }

    [XmlArrayItem("POINTENDID", IsNullable = false)]
    public NetworkROUTEPOINTENDID[] POINTENDIDS { get; set; }

    [XmlAttribute]
    public string ZONE { get; set; }
}

[Serializable]
[XmlType(AnonymousType = true)]
public class NetworkROUTEPOINTENDID
{
    [XmlAttribute]
    public string POS { get; set; }

    [XmlText]
    public string Value { get; set; }
}