可能重複:
LINQ to XML Newbie: Moving Nodes From One Node To Another如何將使用Linq的子節點移動到XML?
我這是通過使用的XDocument它由XElements的產生了以下XML,但我有問題,格式化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>
我想要做的是所有similiar項/後移動到一個類似的節點。這部分代碼:
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>
你有任何你已經嘗試過的代碼樣本? – thekip
你想包含兩個嵌套在一個元素中的同名的XElement? –