2011-10-13 64 views
1

我可以獲取版本日期和版本號以顯示,但對於其餘信息,我希望它輸出元素名稱,然後是標記中的信息。最後,我想它讀的東西,如:如何使用C#在XML文件中循環瀏覽子女的孩子?

versionDate: 2011-10-04 
versionNumber: 1.0 
FirstName: Bob 
LastName: Johnson 
PhoneNumber: 123-456-7890 
FaxNumber: 111-111-1111 
EmailAddress: [email protected] 
Gender: M 
FirstName: Sue 
LastName: Smith 
PhoneNumber: 987-654-3210 
FaxNumber: 222-222-2222 
EmailAddress: [email protected] 
Gender: F 

相反,它顯示的:

versionDate: 2011-10-04 
versionNumber: 1.0 
versionDate#text - 2011-10-04Contact info: False#text - 2011-10-04versionNumber#text - 2011-10-04Contact info: False#text - 2011-10-04ContactFirstName - 2011-10-04Contact info: False#text - 2011-10-04ContactFirstName - 2011-10-04Contact info: False#text - 2011-10-04ContactFirstName - 2011-10-04Contact info: False#text - 2011-10-04ContactFirstName - 2011-10-04Contact info: False#text - 2011-10-04ContactFirstName - 2011-10-04Contact info: 

我試圖使其他的XmlNodeList這是孩子的孩子,但它不是喜歡語法,所以我需要知道如何進入下一級的信息。

我附上了下面的XML和C#文件。

<Contacts> 
    <versionDate>2011-10-04</versionDate> 
    <versionNumber>1.0</versionNumber> 
    <Contact Gender ="M"> 
    <FirstName>Bob</FirstName> 
    <LastName>Johnson</LastName> 
    <PhoneNumber>123-456-7890</PhoneNumber> 
    <FaxNumber>111-111-1111</FaxNumber> 
    <EmailAddress>[email protected]</EmailAddress> 
    </Contact> 
    <Contact Gender ="F"> 
    <FirstName>Sue</FirstName> 
    <LastName>Smith</LastName> 
    <PhoneNumber>987-654-3210</PhoneNumber> 
    <FaxNumber>222-222-2222</FaxNumber> 
    <EmailAddress>[email protected]</EmailAddress> 
    </Contact> 
</Contacts> 


public partial class Form1 : Form 
{ 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    string results = ""; 

    private void button1_Click(object sender, EventArgs e) 
    { 

     string fileName = Application.StartupPath + "\\XMLFile1.xml"; 
     XmlDocument xmlDoc = new XmlDocument(); 

     xmlDoc.Load(fileName); 
     XmlElement elm = xmlDoc.DocumentElement; 

     results += elm.FirstChild.Name + ": " + elm.FirstChild.InnerText + Environment.NewLine; 
     results += elm.FirstChild.NextSibling.Name + ": " + elm.FirstChild.NextSibling.InnerText + Environment.NewLine; 


     XmlNodeList contactInfo = elm.ChildNodes; 
     for (int count = 0; count < contactInfo.Count; count++) 
     { 
      results += (contactInfo[count].Name); 
       results += (contactInfo[count].FirstChild.Name + " - " + contactInfo[0].FirstChild.InnerText); 
      results += ("Contact info: " + contactInfo[0].FirstChild.HasChildNodes.ToString()); 
      XmlNodeList contactProperties = contactInfo[0].ChildNodes; 

      for (int counter = 0; counter < contactProperties.Count; counter++) 
      { 
       results += (contactProperties[counter].Name + " - " + contactProperties[counter].InnerText); 

      } 
     } 

     textBox1.Text += results; 
    } 

} 

任何和所有的幫助將不勝感激!謝謝!

+0

我試圖格式化輸出,但我有點不確定確切的輸出是什麼樣子。請編輯並格式化輸出列表。 –

回答

1

遞歸應該工作:

public string CompileResults(XElement e) 
{ 
    string retVal = String.Format("{0}:{1} ", e.Name, e.Value); 

    foreach (XAttribute xa in e.Attributes()) 
     retVal += String.Format("{0}:{1} ", xa.Name, xa.Value); 

    foreach (XElement xe in e.Elements()) 
     retVal += CompileResults(xe); ; 
    return retVal; 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    string fileName = Application.StartupPath + "\\XMLFile1.xml"; 
    XmlDocument xmlDoc = new XmlDocument(); 
    xmlDoc.Load(fileName); 

    string results = CompileResults(xmlDoc.FirstChild); 
} 
+4

如果你打算制定一個通用的方法,應該考慮StringBuilder。 – canon

+0

在循環中使用'+ ='的StringBuilder的+1是自殺的 –

0

我會做這樣的事情:

public static void DumpXml(XElement root, TextWriter writer) 
{ 
    if (root.HasElements) 
    { 
     foreach (var child in root.Elements()) 
     { 
      DumpXml(child, writer); 
     } 
    } 
    else 
    { 
     writer.WriteLine("{0}: {1}", root.Name, root.Value); 
    } 

    foreach (var attr in root.Attributes()) 
    { 
     writer.WriteLine("{0}: {1}", attr.Name, attr.Value); 
    } 
} 

然後使用它:

var doc = XDocument.Load(xmlPath); 
var writer = new StringWriter(); 
DumpXml(doc.Root, writer); 
var result = writer.ToString(); 
相關問題