我有XML格式如下:導入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>332</SessionTuning_ID>
<Session_ID>33</Session_ID>
<Tuning>Brjesh</Tuning>
<TuningDescr>Brijesh Desc</TuningDescr>
<TuningIsNegative>false</TuningIsNegative>
<TuningAddInfo>33</TuningAddInfo>
<Amp>4.8</Amp>
<Amp2 />
<Amp3 />
<Amp4 />
<Amp5 />
<Amp6 />
<TunFreq>Brjesh</TunFreq>
<TunFreq2 />
<TunFreq3 />
<TunFreq4 />
<TunFreq5 />
<TunFreq6 />
<Revision2>false</Revision2>
<Revision3>false</Revision3>
<Revision4>false</Revision4>
<Revision5>false</Revision5>
<Revision6>false</Revision6>
<AlreadyBalanced>false</AlreadyBalanced>
<ImagePath>E:\Live Projects with Latest Source Code\SE-5\SE-5-Latest-04March11-Multilanguage-Chinese\SE-5\bin\Release\Images\</ImagePath>
<Amp7 />
<TunFreq7 />
<Revision7>0</Revision7>
<Description>Brijesh Note</Description>
<TunRevDate>2013-02-20T18:08:48+05:30</TunRevDate>
<TunRevDate2>2013-02-20T18:08:48+05:30</TunRevDate2>
<TunRevDate3>2013-02-20T18:08:48+05:30</TunRevDate3>
<TunRevDate4>2013-02-20T18:08:48+05:30</TunRevDate4>
<TunRevDate5>2013-02-20T18:08:48+05:30</TunRevDate5>
<TunRevDate6>2013-02-20T18:08:48+05:30</TunRevDate6>
<TunRevDate7>2013-02-20T18:08:48+05:30</TunRevDate7>
<Tuning_ID>20568</Tuning_ID>
</SessionTuning>
....So on
<SessionTuning>
.....
.....
</SessionTuning>
</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>
使用多個SessionTuning
,現在我想在我的字符串數組導入此XML。
我的現有代碼:
public static clsSessionTuningData[] GetSessionTuningFromXMLFile(string FileName)
{
try
{
StreamReader objReader = new StreamReader(FileName);
string strData = objReader.ReadToEnd();
objReader.Close();
int nStart = 0, nEnd = 0;
nStart = strData.IndexOf("<SessionTuningData>");
nEnd = strData.IndexOf("</SessionTuningData>");
if (nStart > 0 && nEnd > 0)
{
nStart = nStart + "<SessionTuningData>".Length;
nEnd = nEnd + "</SessionTuningData>".Length;
strData = strData.Substring(nStart + 1, nEnd - nStart - 1);
string[] strSessionTuning = strData.Split('\r', '\n');
clsSessionTuningData[] objSessionTuning = new clsSessionTuningData[1];
clsDataType.XMLType objXML = new clsDataType.XMLType();
int j = 0;
for (int i = 0; i < strSessionTuning.GetLength(0); i++)
{
objXML = clsCommonFunc.GetXML(strSessionTuning[i]);
if (objXML != null)
{
switch (objXML.Name)
{
case "Revision2":
objSessionTuning[j].bRevision2 = clsCommonFunc.IsYes(objXML.Value);
break;
case "Revision3":
objSessionTuning[j].bRevision3 = clsCommonFunc.IsYes(objXML.Value);
break;
case "Revision4":
objSessionTuning[j].bRevision4 = clsCommonFunc.IsYes(objXML.Value);
break;
case "Revision5":
objSessionTuning[j].bRevision5 = clsCommonFunc.IsYes(objXML.Value);
break;
case "Revision6":
objSessionTuning[j].bRevision6 = clsCommonFunc.IsYes(objXML.Value);
break;
case "TuningIsNegative":
objSessionTuning[j].bTuningIsNegative = clsCommonFunc.IsYes(objXML.Value);
break;
case "Session_ID":
objSessionTuning[j].nSession_ID = int.Parse(objXML.Value);
break;
case "SessionTuning_ID":
objSessionTuning[j] = new clsSessionTuningData();
objSessionTuning[j].nSessionTuning_ID = int.Parse(objXML.Value);
break;
case "TuningAddInfo":
objSessionTuning[j].nTuningAddInfo = int.Parse(objXML.Value);
break;
case "Amp":
objSessionTuning[j].strAmp = objXML.Value;
break;
case "Amp2":
objSessionTuning[j].strAmp2 = objXML.Value;
break;
case "Amp3":
objSessionTuning[j].strAmp3 = objXML.Value;
break;
case "Amp4":
objSessionTuning[j].strAmp4 = objXML.Value;
break;
case "Amp5":
objSessionTuning[j].strAmp5 = objXML.Value;
break;
case "Amp6":
objSessionTuning[j].strAmp6 = objXML.Value;
break;
case "TunFreq":
objSessionTuning[j].strTunFreq = objXML.Value;
break;
case "TunFreq2":
objSessionTuning[j].strTunFreq2 = objXML.Value;
break;
case "TunFreq3":
objSessionTuning[j].strTunFreq3 = objXML.Value;
break;
case "TunFreq4":
objSessionTuning[j].strTunFreq4 = objXML.Value;
break;
case "TunFreq5":
objSessionTuning[j].strTunFreq5 = objXML.Value;
break;
case "TunFreq6":
objSessionTuning[j].strTunFreq6 = objXML.Value;
break;
case "Tuning":
objSessionTuning[j].strTuning = objXML.Value;
break;
case "TuningDescr":
objSessionTuning[j].strTuningDescr = objXML.Value;
break;
case "AlreadyBalanced":
objSessionTuning[j].bAlreadyBalanced = clsCommonFunc.IsYes(objXML.Value);
break;
case "ImagePath":
objSessionTuning[j].strImagePath = objXML.Value;
break;
case "Amp7":
objSessionTuning[j].strAmp7 = objXML.Value; // Krunal on 2-2-2012
break;
case "TunFreq7":
objSessionTuning[j].strTunFreq7 = objXML.Value;
break;
case "Revision7":
objSessionTuning[j].bRevision7 = clsCommonFunc.IsYes(objXML.Value);
break;
case "Description":
objSessionTuning[j].Description = objXML.Value;
break;
case "TunRevDate":
objSessionTuning[j].strRevDate = Convert.ToDateTime(objXML.Value);
break;
case "TunRevDate2":
objSessionTuning[j].strRevDate2 = Convert.ToDateTime(objXML.Value);
break;
case "TunRevDate3":
objSessionTuning[j].strRevDate3 = Convert.ToDateTime(objXML.Value);
break;
case "TunRevDate4":
objSessionTuning[j].strRevDate4 = Convert.ToDateTime(objXML.Value);
break;
case "TunRevDate5":
objSessionTuning[j].strRevDate5 = Convert.ToDateTime(objXML.Value);
break;
case "TunRevDate6":
objSessionTuning[j].strRevDate6 = Convert.ToDateTime(objXML.Value);
break;
case "TunRevDate7":
objSessionTuning[j].strRevDate7 = Convert.ToDateTime(objXML.Value);
break;
case "Tuning_ID":
objSessionTuning[j].nTuning_ID = int.Parse(objXML.Value);
j++;//this is last field so increment by one,
Array.Resize(ref objSessionTuning, j + 1);
break;
}
}
}
return objSessionTuning;
}
return null;
}
catch (Exception ex)
{
clsErrorTrace.CreateLog("SessionTuning", ex);
return null;
}
}
但這個作品,如果我知道最後一個屬性是Tuning_ID
。
有沒有一種簡單的方法去與此..?
哦,我的上帝!你有沒有聽說過有關'Serialization'? – 2013-02-27 13:27:47
是的,我聽說過這個,但對此不知道。 – 2013-02-27 13:28:58
@VishalSuthar我建議你從學習xml序列化開始。它簡化了很多事情:http:// tech。pro/tutorial/798/csharp-tutorial-xml-serialization – Botz3000 2013-02-27 13:33:34