LINQ和的XDocument:如何创建XML文件? [英] LINQ and XDocument: How to create XML file?
问题描述
我在C#中的三甲之列,变量名是 l_lstData1,l_lstData2,l_lstData3
。
I have a three List in c# ,the variable names are 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列出的值如下:
The Values of 3 Lists are as follows :
l_lstData1[0] = "India";
l_lstData1[1] = "chennai";
l_lstData1[2] = "hyderabad";
l_lstData1[3] = "calcutta";
因此,上述XML(元素:数据)的水平属性。有值=2
so the level attribute of the above XML(element : Data) has value="2".
l_lstData2[0] = "vijayawada";
l_lstData2[1] = "cochin";
因此,上述XML(元素:数据)的水平属性。有值=1
so the level attribute of the above XML(element : Data) has value="1".
l_lstData3[0] = "madurai";
l_lstData3[1] = "trichy";
因此,上述XML(元素:数据)的水平属性。有值=0
so the level attribute of the above XML (element: Data) has value="0".
推荐答案
目前尚不清楚确切的为什么的的级别属性符合规定,但是这将创建有关XML为您提供:
It's not clear exactly why the "Level" attributes are as specified, but this would create the relevant XML for you:
// 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_+计数
位使得那个棘手。为什么你需要这样的事情,说实话......如果你能逃脱不说,代码可以清洁目前尚不清楚。
It would probably be neater if you could extract the projections from a list item to its element, but the "Data_" + count
bit makes that tricky. It's not clear why you need such a thing to be honest... if you could get away without that, the code could be cleaner.
我想一个选择是来创建文档的没有的的名称
属性,然后填充它们之后。例如:
I suppose one alternative would be to create the document without the Name
attributes, and then populate them afterwards. For example:
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)));
}
然后:
then:
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++);
}
这篇关于LINQ和的XDocument:如何创建XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!