I am facing some difficulty in swapping the id
attribute in the list of Item
s in a C# XDocument
. The id
is a reference.
Input:
<Items>
<Item base="book_bookref1_ref1">
<Name>Test1</Name>
<Ref>bookref1</Ref>
</Item>
<Item base="book_bookref1_ref2">
<Name>Test2</Name>
<Ref>bookref1</Ref>
</Item>
<Item base="book_bookref2_ref1">
<Name>Test3</Name>
<Ref>bookref2</Ref>
</Item>
<Item base="book_bookref2_ref2">
<Name>Test4</Name>
<Ref>bookref2</Ref>
</Item>
</Items>
Expected Output:
<Items>
<Item base="book_bookref1_ref1" id="book_bookref1_ref2">
<Name>Test1</Name>
<Ref>bookref1</Ref>
</Item>
<Item base="book_bookref1_ref2" id="book_bookref1_ref1">
<Name>Test2</Name>
<Ref>bookref1</Ref>
</Item>
<Item base="book_bookref2_ref1" id="book_bookref2_ref2">
<Name>Test3</Name>
<Ref>bookref2</Ref>
</Item>
<Item base="book_bookref2_ref2" id="bookref2_ref1">
<Name>Test4</Name>
<Ref>bookref2</Ref>
</Item>
</Items>
There is a tag <Ref>
where one part of value ex:bookref1
is a reference for Base attribute book_bookref1_ref1
for the 1st and 2nd Item
.
I need to write the 2nd 'id' to the 1st Item
and write the 1st id
to the 2nd Item
in the loop. Similarly for the 3rd and 4th Item
. There can be many but it's like a combination (1 & 2, 3 & 4, 5 & 6, etc).
The problem here is there can be multiple Item
s and there is no relation in the Item
to map the Id
attribute.
The code I'm using is loading XMLDocument
into XDocument
getting the Item
s in LINQ.
Var doc = XDocument.Load(xml)
var ItemsList
foreach(var itm in ItemsList)
{
// I'm stuck here. How do we get the id attribute value based on Ref tag?
}
Please let me know your suggestions.
Nothing in XML is impossible with a little LINQ (and MoreLinq, in this example):
This enumerates not each
Item
, not eachItem
pair (two elements in a batch). You only need to tweak this to decide what to do if an odd element remains at the end of the loop.MoreLinq is for
.Batch()
extension method, but if you need, you can easily write it yourself.