C# 4.0 使用linq操作XML文档
我需要将以下xml转换为其他形式。我有一个C#代码可以做到这一点,但它有一个很难跟踪的bug。我相信Linq可以提供一种更容易出错的方法。 输入xml:C# 4.0 使用linq操作XML文档,c#-4.0,linq-to-xml,C# 4.0,Linq To Xml,我需要将以下xml转换为其他形式。我有一个C#代码可以做到这一点,但它有一个很难跟踪的bug。我相信Linq可以提供一种更容易出错的方法。 输入xml: <NewDataSet> <Table> <RoleId>5</RoleId> <Code>DP</Code> <Description>Process data</Description> <Task>
<NewDataSet>
<Table>
<RoleId>5</RoleId>
<Code>DP</Code>
<Description>Process data</Description>
<Task>Validate indices</Task>
<TaskId>12</TaskId>
<Country>BE</Country>
<CountryId>3</CountryId>
</Table>
<Table>
<RoleId>5</RoleId>
<Code>DP</Code>
<Description>Process data</Description>
<Task>calculate indices</Task>
<TaskId>11</TaskId>
<Country>US</Country>
<CountryId>4</CountryId>
</Table>
<Table>
<RoleId>5</RoleId>
<Code>DP</Code>
<Description>Process data</Description>
<Task>Calculate indices</Task>
<TaskId>11</TaskId>
<Country>UK</Country>
<CountryId>5</CountryId>
</Table>
<Table>
<RoleId>1</RoleId>
<Code>DR</Code>
<Description>View data</Description>
<Task>View Reports</Task>
<TaskId>9</TaskId>
<Country>SC</Country>
<CountryId>17</CountryId>
</Table>
<Table>
<RoleId>1</RoleId>
<Code>DR</Code>
<Description>View data</Description>
<Task>View Basics</Task>
<TaskId>10</TaskId>
<Country>SC</Country>
<CountryId>17</CountryId>
</Table>
<Table>
<RoleId>1</RoleId>
<Code>DR</Code>
<Description>View data</Description>
<Task>Download data</Task>
<TaskId>11</TaskId>
<Country>FR</Country>
<CountryId>15</CountryId>
</Table>
</NewDataSet>
我需要的输出如下:
<NewDataSet>
<Table>
<RoleId>5</RoleId>
<Code>DP</Code>
<Description>Process data</Description>
<Task>Validate indices,Calculate indices,</Task>
<TaskId>12,11</TaskId>
<Country>BE,US,UK</Country>
<CountryId>3,4,5</CountryId>
</Table>
<Table>
<RoleId>1</RoleId>
<Code>DR</Code>
<Description>Process data from commercial fisheries</Description>
<Task>View Reports,View Basics,View data</Task>
<TaskId>9,10,11</TaskId>
<Country>SC,FR</Country>
<CountryId>17,15</CountryId>
</Table>
</NewDataSet>
如您所见,元素按RoleId、Code和Description进行分组
我已经创建了一个custum对象来将xml元素投影到其中
public class Table
{
public int RoleId {get;set;}
public string Code {get;set;}
public string Description {get;set;}
public string Task {get;set;}
public int TaskId {get;set;}
public string Country {get;set;}
public int CountryId {get;set;}
}
然后,我们可以使用这个custum对象列表来重新创建一个xml文档。但我在想可能会有一种更为严厉的方式。无需使用custum对象列表
元素的其余部分只是连接在一起。我希望有人对如何使用LINQtoXML实现这一点有一些想法。
非常感谢
var doc = XDocument.Load("Input.txt");
var tables = from t in doc.Root.Elements("Table")
select new Table
{
RoleId = (int)t.Element("RoleId"),
Code = (string)t.Element("Code"),
Description = (string)t.Element("Description"),
Task = (string)t.Element("Task"),
TaskId = (int)t.Element("TaskId"),
Country = (string)t.Element("Country"),
CountryId = (int)t.Element("CountryId")
};
var groups = tables.GroupBy(x => new { x.RoleId, x.Code, x.Description });
var resultDoc = new XDocument(
new XElement("NewDataSet",
from g in groups
select new XElement("Table",
new XElement("RoleID", g.Key.RoleId),
new XElement("Code", g.Key.Code),
new XElement("Description", g.Key.Description),
new XElement("Task", string.Join(",", g.Select(x => x.Task))),
new XElement("TaskId", string.Join(",", g.Select(x => x.TaskId.ToString()))),
new XElement("Country", string.Join(",", g.Select(x => x.Country))),
new XElement("CountryId", string.Join(",", g.Select(x => x.CountryId.ToString()))))));
完全是LINQ到XML解决方案,尽管您应该考虑用XML序列化来改变解析部分。 结果XML:
<NewDataSet>
<Table>
<RoleID>5</RoleID>
<Code>DP</Code>
<Description>Process data</Description>
<Task>Validate indices,calculate indices,Calculate indices</Task>
<TaskId>12,11,11</TaskId>
<Country>BE,US,UK</Country>
<CountryId>3,4,5</CountryId>
</Table>
<Table>
<RoleID>1</RoleID>
<Code>Data Reader</Code>
<Description>View data</Description>
<Task>View Reports,View Basics,Download data</Task>
<TaskId>9,10,11</TaskId>
<Country>SC,SC,FR</Country>
<CountryId>17,17,15</CountryId>
</Table>
</NewDataSet>
你应该在这里展示你的努力:你尝试过的代码,等等。谢谢Marcin,我怎样才能应用disticnt,以便在连接的元素中只返回唯一的值?对不起,我找到了。刚刚在select上再次应用了distinct,非常感谢。