如何使用Linq将子节点移动到XML?

时间:2011-09-21 11:47:38

标签: c# xml linq-to-xml

  

可能重复:
  LINQ to XML Newbie: Moving Nodes From One Node To Another

我有以下XML,它是使用由XElements组成的XDocument生成的,但我在格式化XML方式时遇到了问题:

这就是我现在所拥有的:

<Root>
 <Companies>
  <Company>
   <ID>1</ID)
   <Name>Kalle</Name>
   <RegNo>1111</RegNo>
  </Company>
 </Companies>
 <Companies>
  <Company>
   <ID>1</ID)
   <Name>Kalle</Name>
   <RegNo>1112</RegNo>
  </Company>
 </Companies>  
</root>

这就是我想要的:

   <Root>
    <Companies>
      <Company>
       <ID>1</ID)
       <Name>Kalle</Name>
       <RegNo>1111</RegNo>
       <RegNo>1112</RegNo>
      </Company>
    </Companies>  
   </Root>

我想要做的是将所有类似的条目/帖子移动到类似的节点。这部分代码:

XElement xmlTree = new XElement("Root", File.ReadAllLines("C:\\Data.txt")

         .Select
         (
             line =>
             {
                 var split = line.DelSplit();
                 return new XElement("Companies",
                     new XElement("Company", split[0]),
                     new XElement("ID",
                     new XElement("Name",
                     new XElement("RegNo", split[1])
              );


             }



         )

XDocument Xdoc = new XDocument(xmlTree);
Xdoc.Save("c:\\Data.xml");

以下是我想要合并的原始资料来源:

<Club>
    <ID>A</ID>
    <ContractYears>
      <ContractYear>
        <Year>2011</Year>
        <Owners>
          <Owner>
            <OwnerName>Walt Disney</OwnerName>
            <RegistrationNumber>131313</RegistrationNumber>
            <Vessels>
              <Vessel>
                <VesselName>Alpha</VesselName>
                <Status>A</Status>
                <DateAttached></DateAttached>
                <BrokerName></BrokerName>
                <Currencies>
                  <Currency>
                    <CurrencyCode></CurrencyCode>
                    <TotalValue></TotalValue>
                    <InterestSplits>
                      <InterestSplit>
                        <Number></Number>
                        <Amount></Amount>
                      </InterestSplit>
                    </InterestSplits>
                  </Currency>
                </Currencies>
              </Vessel>
            </Vessels>
          </Owner>
        </Owners>
      </ContractYear>
    </ContractYears>
  </Club>
  <Club>
    <ID>A</ID>
    <ContractYears>
      <ContractYear>
        <Year>2011</Year>
        <Owners>
          <Owner>
            <OwnerName>Walt Disney.</OwnerName>
            <RegistrationNumber>131313</RegistrationNumber>
            <Vessels>
              <Vessel>
                <VesselName>Beta</VesselName>
                <Status></Status>
                <DateAttached></DateAttached>
                <BrokerName></BrokerName>
                <Currencies>
                  <Currency>
                    <CurrencyCode></CurrencyCode>
                    <TotalValue></TotalValue>
                    <InterestSplits>
                      <InterestSplit>
                        <Number></Number>
                        <Amount></Amount>
                      </InterestSplit>
                    </InterestSplits>
                  </Currency>
                </Currencies>
              </Vessel>
            </Vessels>
          </Owner>
        </Owners>
      </ContractYear>
    </ContractYears>
  </Club>
  <Club>

1 个答案:

答案 0 :(得分:0)

如果您想从文本文件中创建正确的XML,那么您将向我们展示文本文件中的数据的外观。 假设您想要操作现有的XML,您可以使用LINQ分组和LINQ to XML来实现这一点。

            XDocument input = XDocument.Parse(@"<Root>
 <Companies>
  <Company>
   <ID>1</ID>
   <Name>Kalle</Name>
   <RegNo>1111</RegNo>
  </Company>
 </Companies>
 <Companies>
  <Company>
   <ID>1</ID>
   <Name>Kalle</Name>
   <RegNo>1112</RegNo>
  </Company>
 </Companies>  
</Root>
");
            XDocument output =
                new XDocument(
                    new XElement(input.Root.Name,
                        new XElement("Companies",
                            from comp in input.Root.Elements("Companies").Elements("Company")
                            group comp by (int)comp.Element("ID") into g
                            select new XElement("Company",
                                new XElement("ID", g.Key),
                                g.Elements("Name").First(),
                                g.Elements("RegNo")
                            ))));

            output.Save(Console.Out);

输出

<Root>
  <Companies>
    <Company>
      <ID>1</ID>
      <Name>Kalle</Name>
      <RegNo>1111</RegNo>
      <RegNo>1112</RegNo>
    </Company>
  </Companies>
</Root>

如果您想在从文本文件中读取这些行时对数据进行分组,那么LINQ“from .. group by into select”当然会非常相似。