2013-08-27 95 views
0

我有以下xml,我需要transfom到其他窗體。我有一個C#代碼,但它有一個難以跟蹤的錯誤。我相信Linq可以提供更容易出錯的方式來做到這一點。 輸入XML:用linq處理XML文檔

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

正如你所看到的,元素是角色ID,代碼和描述組。

我創建了一個對象俗到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;} 

} 

的想法是再使用對象俗此列表重新創建XML文檔。但我認爲可能會有更直接的方式。而不需要使用custum對象列表。

元素的其餘部分是簡單的連接。我希望有人對如何使用Linq to XML實現這一點有個想法。 許多在此先感謝

+0

你一定要在這裏展示您的努力:代碼你試過等 – MarcinJuraszek

回答

1
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 to 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> 
+0

謝謝馬爾欽,我怎麼能aplly這樣只有獨特的價值在級聯ellement返回disticnt ? –

+0

對不起,我找到了。只是在選擇上應用了一個獨特的,非常感謝。 –