Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要使用循环将记录存储在Xml子级中_C#_Xml_Linq To Xml - Fatal编程技术网

C# 需要使用循环将记录存储在Xml子级中

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",

我需要将列表中的数据添加到xml文件中。我正在使用XDocument和creating元素以xml格式创建和存储数据。现在我有多个,我试图使用foreach循环来存储个人数据STAFFID,但它给了我错误

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循环区域