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# LINQ和XDocument:如何创建XML文件?_C#_Xml_Linq_Linq To Xml - Fatal编程技术网

C# LINQ和XDocument:如何创建XML文件?

C# LINQ和XDocument:如何创建XML文件?,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我在c#中有三个列表,变量名是l_lstData1,l_lstData2,l_lstData3 文件结构是 <FileDetails> <Date FileModified="29/04/2010 12:34:02" /> <Data Name="Data_1" DataList="India" Level="2" /> <Data Name="Data_2" DataList="chennai" Level="2" />

我在c#中有三个列表,变量名是
l_lstData1,l_lstData2,l_lstData3

文件结构是

<FileDetails>  
  <Date FileModified="29/04/2010 12:34:02" />   
  <Data Name="Data_1" DataList="India" Level="2" />   
  <Data Name="Data_2" DataList="chennai" Level="2" />   
  <Data Name="Data_3" DataList="hyderabad" Level="2" />   
  <Data Name="Data_4" DataList="calcutta" Level="2" />  
  <Data Name="Data_5" DataList="vijayawada" Level="1" /> 
  <Data Name="Data_6" DataList="cochin" Level="1" /> 
  <Data Name="Data_7" DataList="madurai" Level="0" />  
  <Data Name="Data_8" DataList="trichy" Level="0" />   
</FileDetails>
因此,上述XML(元素:Data)的level属性具有value=“2”

因此,上述XML(元素:Data)的level属性具有value=“1”

因此,上述XML(element:Data)的level属性具有value=“0”。

不清楚为什么会指定“level”属性,但这将为您创建相关的XML:

// Used for side-effects in the XElement constructor. This is a bit icky. It's
// not clear what the "Name" part is really for...
int count = 1;

var doc = new XDocument(
    new XElement("FileDetails",
        new XElement("Date", new XAttribute("FileModified", DateTime.UtcNow)),
        l_lstData1.Select(x => new XElement("Data",
            new XAttribute("Name", "Data_" + count++),
            new XAttribute("DataList", x),
            new XAttribute("Level", 2))),
        l_lstData2.Select(x => new XElement("Data",
            new XAttribute("Name", "Data_" + count++),
            new XAttribute("DataList", x),
            new XAttribute("Level", 1))),
        l_lstData3.Select(x => new XElement("Data",
            new XAttribute("Name", "Data_" + count++),
            new XAttribute("DataList", x),
            new XAttribute("Level", 0)))));
如果您可以将投影从列表项提取到其元素,可能会更整洁,但是
的“Data_”+count
位使这变得棘手。不清楚你为什么需要这样的东西来诚实。。。如果你没有这些,代码可能会更干净

我认为另一种方法是创建不带
Name
属性的文档,然后填充它们。例如:

private static IEnumerable<XElement> ProjectList(IEnumerable<string> list,
    int level)
{
    return list.Select(x => new XElement("Data",
        new XAttribute("DataList", x),
        new XAttribute("Level", level)));
}
那么:

XDocument doc = new XDocument();

var total = (from a in list1 select new { Name = a, Level = 2 }).Concat(
             from b in list2 select new { Name = b, Level = 1 }).Concat(
             from c in list3 select new { Name = c, Level = 0 });

XElement root = new XElement("FileDetails", from i in Enumerable.Range(0, total.Count())
                                            let element = total.ElementAt(i)
                                            let name = new XAttribute("Name", String.Format("Data_{0}", i + 1))
                                            let level = new XAttribute("Level", element.Level)
                                            let datalist = new XAttribute("DataList", element.Name)
                                            select new XElement("Data", name, datalist, level),
                                            new XElement("Date", new XAttribute("FileModified", DateTime.Now)));
// Used for side-effects in the XElement constructor. This is a bit icky. It's
// not clear what the "Name" part is really for...
int count = 1;

var doc = new XDocument(
    new XElement("FileDetails",
        new XElement("Date", new XAttribute("FileModified", DateTime.UtcNow)),
        l_lstData1.Select(x => new XElement("Data",
            new XAttribute("Name", "Data_" + count++),
            new XAttribute("DataList", x),
            new XAttribute("Level", 2))),
        l_lstData2.Select(x => new XElement("Data",
            new XAttribute("Name", "Data_" + count++),
            new XAttribute("DataList", x),
            new XAttribute("Level", 1))),
        l_lstData3.Select(x => new XElement("Data",
            new XAttribute("Name", "Data_" + count++),
            new XAttribute("DataList", x),
            new XAttribute("Level", 0)))));
private static IEnumerable<XElement> ProjectList(IEnumerable<string> list,
    int level)
{
    return list.Select(x => new XElement("Data",
        new XAttribute("DataList", x),
        new XAttribute("Level", level)));
}
var doc = new XDocument(
    new XElement("FileDetails",
        new XElement("Date", new XAttribute("FileModified", DateTime.UtcNow)),
        ProjectList(l_lstData1, 2),
        ProjectList(l_lstData2, 1),
        ProjectList(l_lstData3, 0)));

int count = 1;
foreach (var element in doc.Descendants("Data"))
{
    element.SetAttributeValue("Name", "Data_" + count++);
}
XDocument doc = new XDocument();

var total = (from a in list1 select new { Name = a, Level = 2 }).Concat(
             from b in list2 select new { Name = b, Level = 1 }).Concat(
             from c in list3 select new { Name = c, Level = 0 });

XElement root = new XElement("FileDetails", from i in Enumerable.Range(0, total.Count())
                                            let element = total.ElementAt(i)
                                            let name = new XAttribute("Name", String.Format("Data_{0}", i + 1))
                                            let level = new XAttribute("Level", element.Level)
                                            let datalist = new XAttribute("DataList", element.Name)
                                            select new XElement("Data", name, datalist, level),
                                            new XElement("Date", new XAttribute("FileModified", DateTime.Now)));