1

我正在创建一个 xml 架构

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">
<Inventories>
<ID>           </ID>
<Category>     </Category>
<Identity>     </Identity>
<Name>         </Name>
</Inventories>
</string>

使用以下代码

resultList = remoteWeb.Lists.GetByTitle("Server");
context.Load(resultList);
context.ExecuteQuery();
items = resultList.GetItems(new CamlQuery());
context.Load(items);
context.ExecuteQuery();
foreach (ListItem item in items)
{
    rootNode.AppendChild(doc.CreateElement("ID")).InnerText = "pcat:401820";
    rootNode.AppendChild(doc.CreateElement("Category")).InnerText = "Infrastructure.Server";
    rootNode.AppendChild(doc.CreateElement("Identity")).InnerText = Convert.ToString(item["ID"]);
    rootNode.AppendChild(doc.CreateElement("Name")).InnerText = Convert.ToString(item["Server_Name"]) + " >> " + Convert.ToString(item["Computer_Name"]) + " >> " + Convert.ToString(item["IP"]);
}

但是我们需要将xml设置为

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">
<Inventories>
<Inventory>
<ID>            </ID>
<Category>      </Category>
<Identity>      </Identity>
<Name>          </Name>
</Inventory>
</Inventories>

但没有弄清楚如何在下添加容器节点

所有帮助将不胜感激,并在此先感谢

4

1 回答 1

0
List<Inventory> items = new List<Inventory>();
items.Add(new Inventory() { ID = "id1", Category = "c1", Identity = "i1", Name = "n1" });
items.Add(new Inventory() { ID = "id2", Category = "c2", Identity = "i2", Name = "n2" });

XmlSerializer xml = new XmlSerializer(typeof(List<Inventory>),new XmlRootAttribute("Inventories"));
xml.Serialize(stream, items);

-

public class Inventory
{
    public string ID;
    public string Category;
    public string Identity;
    public string Name;
}
于 2012-07-18T06:13:51.910 回答