-2

我有一个这样的 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。?

4

3 回答 3

1

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

我本来希望某些[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; }
}
于 2013-03-01T10:06:08.940 回答
0

您可以使用像 xsd.exe 这样的工具来创建一个 xsd 文件,然后您可以从这里使用序列化

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

它可能不会创建完全相同的 xml,但它会很接近......

顺便说一句,使用 newtonsoft json 序列化为 json 而不是 xml 可能更容易

于 2013-03-01T10:06:52.373 回答
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 )

于 2013-03-01T10:10:20.233 回答