如何读取XML到类对象

本文关键字:XML 对象 读取 何读取 | 更新日期: 2023-09-27 18:01:46

我有一个这样的XML:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<Session>
<bIsImages>False</bIsImages>
<bIsPlayMedia>False</bIsPlayMedia>
<bIsSubject>False</bIsSubject>
<bIsVideo>False</bIsVideo>
<dtCreDate>2012-07-23</dtCreDate>
<dtSes_Date1>2001-01-01</dtSes_Date1>
<dtSes_Date2>2001-01-01</dtSes_Date2>
<dtSes_Date3>2001-01-01</dtSes_Date3>
<nClient_ID>32</nClient_ID>
<nDelay>32</nDelay>
<nImage_ID>32</nImage_ID>
<nOperator_ID>32</nOperator_ID>
<nSession_ID>32</nSession_ID>
<nVitality>32</nVitality>
<strDescr>qi stagnatie abq</strDescr>
<strMediaPath></strMediaPath>
<strName>qi stagnatie abq</strName>
<strPrimCause></strPrimCause>
<strSubjectPath>IDF_Eric duBosc.JPG</strSubjectPath>
</Session>
<SessionProgramData>
</SessionProgramData><SessionSubProgramData>
</SessionSubProgramData><SessionTuningData>
  <SessionTuning>
    <SessionTuning_ID>717</SessionTuning_ID>
    <Session_ID>70</Session_ID>
    <Tuning>8285-100</Tuning>
    <TuningDescr>Disturbance by Qistagnatie in general</TuningDescr>
    <TuningIsNegative>true</TuningIsNegative>
    <TuningAddInfo>70</TuningAddInfo>
    <Amp>2.2</Amp>
    <Amp2 />
    <Amp3>0.1</Amp3>
    <Amp4>5.1</Amp4>
    <Amp5>1.0</Amp5>
    <Amp6 />
    <TunFreq>8285-100</TunFreq>
    <TunFreq2 />
    <TunFreq3>8285-100</TunFreq3>
    <TunFreq4 />
    <TunFreq5 />
    <TunFreq6 />
    <Revision2>false</Revision2>
    <Revision3>false</Revision3>
    <Revision4>false</Revision4>
    <Revision5>false</Revision5>
    <Revision6>false</Revision6>
    <AlreadyBalanced>false</AlreadyBalanced>
    <ImagePath />
    <Amp7 />
    <TunFreq7 />
    <Revision7>0</Revision7>
    <Description />
  </SessionTuning>
  ....So on
</SessionTuningData>
<Client>
<nClient_ID>32</nClient_ID>
<strAddress></strAddress>
<strCity></strCity>
<strCountry></strCountry>
<strFirstName>Eric</strFirstName>
<strImage>IDF_Eric duBosc.JPG</strImage>
<strLastName>Bosc</strLastName>
<strMI>du</strMI>
<strNote>ikke</strNote>
<strPhoneNum></strPhoneNum>
<strPostalCode></strPostalCode>
<strState></strState>
<strWorkPhone></strWorkPhone>
</Client>
<SE-5 />
</DocumentElement>

和类如下:

    public class clsSessionTuningData
    {
        public int nSessionTuning_ID;
        public int nSession_ID;
        public int nTuning_ID;
        public string strTuning;
        public string strTuningDescr;
        public string Description;
        public bool bTuningIsNegative;
        public int nTuningAddInfo;
        public string strAmp;
        public string strAmp2;
        public string strAmp3;
        public string strAmp4;
        public string strAmp5;
        public string strAmp6;
        public string strAmp7;
        public DateTime strRevDate;
        public DateTime strRevDate2;
        public DateTime strRevDate3;
        public DateTime strRevDate4;
        public DateTime strRevDate5;
        public DateTime strRevDate6;
        public DateTime strRevDate7;
        public string strTunFreq;
        public string strTunFreq2;
        public string strTunFreq3;
        public string strTunFreq4;
        public string strTunFreq5;
        public string strTunFreq6;
        public string strTunFreq7;
        public bool bRevision2;
        public bool bRevision3;
        public bool bRevision4;
        public bool bRevision5;
        public bool bRevision6;
        public bool bRevision7;
        public bool bAlreadyBalanced;
        public string strImagePath;
    }

那么我如何在clsSessionTuningData对象的数组中获得<SessionTuningData>的每个标签的值?

如何读取XML到类对象

你需要把你的类标记为可序列化的

我希望一些[XmlElement]属性至少出现在任何读过教程的人的代码中。

就像这个

public class Movie
{
  [XmlElement("MovieName")]
  public string Title
  { get; set; }
  [XmlElement("MovieRating")]
  public float Rating
  { get; set; }
  [XmlElement("MovieReleaseDate")]
  public DateTime ReleaseDate
  { get; set; }
}

你可以使用像xsd.exe这样的工具创建一个xsd文件,从这里你可以使用

序列化http://snipplr.com/view/2660/

它可能不会创建完全相同的XML,但它会接近…

顺便说一下,使用newtonsoft json

序列化为json可能比使用xml更容易。

有很多方法可以做到这一点。

  1. 用xml属性装饰你的类属性(例如:XmlElement, XmlAttribute) (http://www.codeproject.com/Articles/14064/Using-the-XmlSerializer-Attributes)

  2. 实现IXmlSerializable (http://www.codeproject.com/Articles/43237/How-to-Implement-IXmlSerializable-Correctly)

  3. 或者使用DataContractSerializer (http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx)