我想創建嵌套的XML,這樣的結果會是這樣的:如何在C#中嵌套的XML
<?xml version=\"1.0\" encoding=\"UTF-8\"?><TASKLOADLOG>
<PERSON>
<EMAIL>data</EMAIL><LOADED>OK</LOADED><LOADERROR>ABC</LOADERROR>
</PERSON>
<PERSON>
<EMAIL>data</EMAIL><LOADED>OK</LOADED><LOADERROR>ABC</LOADERROR>
</PERSON>
<PERSON>
<EMAIL>data</EMAIL><LOADED>OK</LOADED><LOADERROR>ABC</LOADERROR>
</PERSON>
</TASKLOADLOG>"
我寫了下面的代碼,並墜毀在一個循環
XmlDocument XmlResponse = new XmlDocument();
XmlDeclaration xDeclare = XmlResponse.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement documentRoot = XmlResponse.DocumentElement;
XmlResponse.InsertBefore(xDeclare, documentRoot);
XmlElement el = (XmlElement)XmlResponse.AppendChild(XmlResponse.CreateElement("TASKLOADLOG"));
List<XmlElement> ls = new List<XmlElement>();
for (int i = 0; i < 3; i++)
{
ls[i].AppendChild(XmlResponse.CreateElement("EMAIL")).InnerText = "data";
ls[i].AppendChild(XmlResponse.CreateElement("LOADED")).InnerText = "OK";
ls[i].AppendChild(XmlResponse.CreateElement("LOADERROR")).InnerText = "ABC";
}
MessageBox.Show(XmlResponse.OuterXml);
我不現在如何定義PERSON
我需要寫什麼來修復我的代碼?
你應該解釋該代碼與OP代碼的區別。 – Enigmativity