如何将XML读取到类对象

时间:2013-03-01 09:57:11

标签: c# xml deserialization

我有这样的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;
    }

那么如何在<SessionTuningData>对象的数组中获取clsSessionTuningData的每个标记的值。?

3 个答案:

答案 0 :(得分:1)

您需要将您的课程标记为可序列化。

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

Like this one

public class Movie
{
  [XmlElement("MovieName")]
  public string Title
  { get; set; }

  [XmlElement("MovieRating")]
  public float Rating
  { get; set; }

  [XmlElement("MovieReleaseDate")]
  public DateTime ReleaseDate
  { get; set; }
}

答案 1 :(得分:0)

您可以使用xsd.exe之类的工具创建xsd文件,然后可以使用

进行序列化

http://snipplr.com/view/2660/

它可能不会创建完全相同的xml,但它会关闭......

另外,使用newtonsoft json

可能更容易序列化为json而不是xml

答案 2 :(得分:0)

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

  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