2013-02-27 75 views
1

我有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

有沒有一種簡單的方法去與此..?

+2

哦,我的上帝!你有沒有聽說過有關'Serialization'? – 2013-02-27 13:27:47

+0

是的,我聽說過這個,但對此不知道。 – 2013-02-27 13:28:58

+0

@VishalSuthar我建議你從學習xml序列化開始。它簡化了很多事情:http:// tech。pro/tutorial/798/csharp-tutorial-xml-serialization – Botz3000 2013-02-27 13:33:34

回答

0

,你可以:

1)您的XML加載到一個XMLDocument,再通過其屬性(主要是ChildNodes)瀏覽文檔; 2)將XML加載到XMLDocument中,然後使用XPath查詢文檔,通過CreateNavigator創建導航器。

3)用XmlReader解析文檔。

4)創建一組與Xml架構密切相關的類,並使用XmlDeserializer將文件映射到一組對象(這很好,但設置起來很棘手,因爲XmlDeserializer可能有點頑固配置)。

如果我需要所有的數據,我會選擇(4),可能重新設計我的XML /模型類,使轉換更容易。

如果我需要一些值,我可能會去(2),因爲它只需要很少的代碼。 查看(2)here的一些示例。

2

當然有更簡單的方法來做到這一點。其中一個名爲Serialization,您可以將整個類轉換爲某種類型的流,即XML和字節。你使用XmlSerializer這個類。下面是一個簡單的例子:

假設你有,你要存儲在一個類和XML文件:

[Serializable] // you should mark the class as serializable 
public class MyData 
{ 
    public int Value { get; set; } 
    public string Name { get; set; } 
    public string[] SubItems { get; set; } 
} 

這個代碼把它轉換成XML文件:

MyData data = new MyData(); 
data.Name = "TestName"; 
data.Value = 100; 
data.SubItems = new string[] {"Item1", "Item2", "Item3"}; 
XmlSerializer ser = new XmlSerializer(typeof(MyData)); 
using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write)) 
{ 
    ser.Serialize(file, data); 
} 

這裏生成的文件:

<?xml version="1.0"?> 
<MyData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Value>100</Value> 
    <Name>TestName</Name> 
    <SubItems> 
    <string>Item1</string> 
    <string>Item2</string> 
    <string>Item3</string> 
    </SubItems> 
</MyData> 

讀取該文件,您可以使用Deserialize方法:

using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read)) 
{ 
    ser.Deserialize(file, data); 
} 

另一種選擇是使用專門的類別中的一個.NET框架讀取XML文件:

XDocument doc = XDocument.Load(fileName); 
List<clsSessionTuningData> objSessionTuning = new List<clsSessionTuningData>() 
foreach (var session in doc.Descendants("SessionTuningData")) 
{ 
    XElement elem; 
    clsSessionTuningData data = new clsSessionTuningData(); 
    elem = session.Element("Revision2"); 
    if (elem != null) 
     data.bRevision2 = elem.Value; 

    // and so on... 
}