How can I move child nodes using Linq to XML? [duplicate]
Possible Duplicate:
LINQ to XML Newbie: Moving Nodes From One Node To Another
I have the following XML which is produced by using XDocument which consists of XElements, but I have problems to formatting the XML way I want it to be:
This is what I have now:
<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>
This is what I want:
<Root>
<Companies>
<Company>
<ID>1</ID)
<Name>Kalle</Name>
<RegNo>1111</RegNo>
<RegNo>1112</RegNo>
</Company>
</Companies>
</Root>
What I want to do is to move all similiar entries / post to a similiar node. This some of the code:
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");
Here is the orginal source which I want to combine:
<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>
Well if you want to create the right XML from your text file you will to show us how the data in the text files looks. Assuming you want to manipulate the existing XML you can achieve that with a LINQ grouping and LINQ to XML e.g.
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);
outputs
<Root>
<Companies>
<Company>
<ID>1</ID>
<Name>Kalle</Name>
<RegNo>1111</RegNo>
<RegNo>1112</RegNo>
</Company>
</Companies>
</Root>
The LINQ "from .. group by into .. select" would of course be quite similar if you wanted to group the data when reading in those lines from the text file.
精彩评论