對不起,我對XML API不太好。我將如何生成以下格式的XML文件並寫入它?如何生成並寫入此格式的XML文件?
<?xml version="1.0" encoding="utf-8" ?>
<ROOT>
<LOC ID="*">
<ROW ID = "1" CD = "US" DESC = "United States" ISACTIVE="1" ORDER="1"/>
<ROW ID = "2" CD = "CA" DESC = "Canada" ISACTIVE="1" ORDER="2"/>
<ROW ID = "3" CD = "XX" DESC = "Others" ISACTIVE="1" ORDER="3"/>
</LOC>
</ROOT>
這是我最好的第一次嘗試。硬編碼值將不得不由數據庫中的值替換。我不知道如何迭代和創建多行元素。
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("ROOT");
xmlDoc.AppendChild(rootNode);
XmlNode locNode = xmlDoc.CreateElement("LOC");
XmlAttribute attr = xmlDoc.CreateAttribute("ID");
attr.Value = "*";
rootNode.AppendChild(locNode);
XmlNode rowNode = xmlDoc.CreateElement("ROW");
XmlAttribute id = xmlDoc.CreateAttribute("CD");
id.Value = "1";
XmlAttribute cd = xmlDoc.CreateAttribute("CD");
cd.Value = "US";
XmlAttribute desc = xmlDoc.CreateAttribute("DESC");
desc.Value = "United States";
XmlAttribute active = xmlDoc.CreateAttribute("ISACTIVE");
active.Value = "1";
XmlAttribute order = xmlDoc.CreateAttribute("ORDER");
order.Value = "1";
rootNode.AppendChild(rowNode);
xmlDoc.Save("foo.xml");
讓我們來看看你到目前爲止有多遠? – musefan 2014-10-27 16:37:17
發佈你的最佳嘗試,讓我們知道問題是什麼。 – nvoigt 2014-10-27 16:37:27
[在C#代碼中構建XML的最佳方式是什麼?](http://stackoverflow.com/questions/284324/what-is-the-best-way-to-build-xml-in-c -sharp-code) – 2014-10-27 16:56:50