【问题标题】:How to create a XML file using XDocument and LINQ in C#?如何在 C# 中使用 XDocument 和 LINQ 创建 XML 文件?
【发布时间】:2010-06-25 04:50:39
【问题描述】:

我在c#中有一个三个List,变量名是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>

3 个列表的值如下:

  l_lstData1[0] = "India";l_lstData1[1] = "chennai";l_lstData1[2] = "hyderabad";
  l_lstData1[3] = "calcutta"; 

所以上述 XML(element : Data) 的 level 属性的值 = "2"。

  l_lstData2[0] = "vijayawada";l_lstData2[1] = "cochin";      

所以上述 XML(element : Data) 的 level 属性的值 = "1"。

 l_lstData3[0] = "madurai";l_lstData3[1] = "trichy";      

所以上述 XML(element : Data) 的 level 属性的值 = "0"。

如何使用 Xdocument 和 LINQ 创建 XML....如果您有任何疑问,请回复我

【问题讨论】:

  • 你说:所以上面 XML(element : Data) 的 level 属性有 tha value = "2" (对于其他值也是如此) - 但我不不明白为什么它会“如此”。仅仅因为它在哪个列表中?还是因为别的原因?

标签: c#


【解决方案1】:

这是 Pramodh 解决方案的替代方案,如果我理解正确:

// First build up a single list to work with, using an anonymous type
var singleList = l_lstData1.Select(x => new { Value = x, Level = 2})
         .Concat(l_lstData2.Select(x => new { Value = x, Level = 1})
         .Concat(l_lstData3.Select(x => new { Value = x, Level = 0});

var doc = new XDocument(
    new XElement("FileDetails",
        new XElement("Date",new XAttribute("FileModified", DateTime.Now)),
        singleList.Select((item, index) => new XElement("Data",
            new XAttribute("Name", "Data_" + (index + 1)),
            new XAttribute("DataList", item.Value),
            new XAttribute("Level", item.Level))));

【讨论】:

  • skeet:很简单。谢谢你
【解决方案2】:

试试这样:

           XDocument TEMP = new XDocument(new XElement("FileDetails",
                                          new XElement("Date",new XAttribute("FileModified", DateTime.Now.ToString())),
                   l_lstData1.Select(l => new XElement("Data",new XAttribute("Name","Data_"+(l_lstData1.IndexOf(l)+1).ToString()),
                                                              new XAttribute ("DataList",l.ToString()),
                                                              new XAttribute ("Level","Level2"))),
                   l_lstData2.Select(l => new XElement("Data",new XAttribute("Name","Data_"+(l_lstData2.Count + l_lstData2.IndexOf(l)+1).ToString()),
                                                              new XAttribute ("DataList",l.ToString()),
                                                              new XAttribute ("Level","Level1"))) ,
                   l_lstData3.Select(l => new XElement("Data",new XAttribute("Name", "Data_" + (l_lstData3.Count + l_lstData2.Count + l_lstData3.IndexOf(l) + 1).ToString()),
                                                              new XAttribute ("DataList",l.ToString()),
                                                              new XAttribute ("Level","Level0")))                                                                 

                                                        ));


        TEMP.Save("TEMP.xml");

【讨论】:

    猜你喜欢
    • 2011-06-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 2012-10-13
    • 1970-01-01
    • 2010-11-03
    相关资源
    最近更新 更多