Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

c# - How to Read XML to Class Object

I have an XML like this:

<?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>

And the class as below:

    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;
    }

So How can I get values for each tag of <SessionTuningData> in array of clsSessionTuningData object .?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to mark your class as serializable.

I would have expected some [XmlElement] attributes at least to appear in the code of anyone who had read any tutorial.

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; }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...