对不起,我对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