C# 需要使用循环将记录存储在Xml子级中
我需要将列表中的数据添加到xml文件中。我正在使用XDocument和creating元素以xml格式创建和存储数据。现在我有多个,我试图使用foreach循环来存储个人数据STAFFID,但它给了我错误C# 需要使用循环将记录存储在Xml子级中,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,我需要将列表中的数据添加到xml文件中。我正在使用XDocument和creating元素以xml格式创建和存储数据。现在我有多个,我试图使用foreach循环来存储个人数据STAFFID,但它给了我错误 public void generateXMLFile(List<UWL> myList ) { XDocument objXDoc = new XDocument( new XElement("Institution",
public void generateXMLFile(List<UWL> myList )
{
XDocument objXDoc = new XDocument(
new XElement("Institution",
new XElement("RECID", myList[0].recid),
new XElement("UKPRN", myList[0].UKPRN),
new XElement("Person",
foreach(var m in myList)
{
new XElement("STAFFID", m.STAFFID)
}
)
)
);
objXDoc.Declaration = new XDeclaration("1.0", "utf-8", "true");
//
objXDoc.Save(@"C:\Test\generated.xml");
//Completed.......//
MessageBox.Show("Process Completed......");
}
public void generateXMLFile(列表myList)
{
XDocument objXDoc=新XDocument(
新XElement(“机构”,
新XElement(“RECID”,myList[0]。RECID),
新XElement(“UKPRN”,myList[0].UKPRN),
新XElement(“人”,
foreach(myList中的var m)
{
新XElement(“斯塔夫”,m.STAFFID)
}
)
)
);
objXDoc.Declaration=newxdeclaration(“1.0”、“utf-8”、“true”);
//
objXDoc.Save(@“C:\Test\generated.xml”);
//已完成//
MessageBox.Show(“流程已完成……”);
}
您需要为Person
元素提供内容。Foreach循环不返回任何内容。因此,有效代码为:
XDocument objXDoc = new XDocument(
new XElement("Institution",
new XElement("RECID", myList[0].recid),
new XElement("UKPRN", myList[0].UKPRN),
new XElement("Person",
myList.Select(m => new XElement("STAFFID", m.STAFFID))
)
)
);
这将创建
STAFFID
元素的集合,并将此集合设置为Person
元素的内容。让我猜猜您有哪一个错误..它将显示在foreach循环区域