我可以獲取版本日期和版本號以顯示,但對於其餘信息,我希望它輸出元素名稱,然後是標記中的信息。最後,我想它讀的東西,如:如何使用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;
}
}
任何和所有的幫助將不勝感激!謝謝!
我試圖格式化輸出,但我有點不確定確切的輸出是什麼樣子。請編輯並格式化輸出列表。 –