I am trying to insert these nodes price, qty, amount under the descendants Apple > Store only using the attribute to find the exact location of insert.
<?xml version="1.0" encoding="utf-8"?>
<Fruits>
<node text="Apple" tag="a" imageindex="0">
<node text="Store" tag="b" imageindex="1" />
<node text="City" tag="c" imageindex="2" />
</node>
<node text="Orange" tag="a" imageindex="0">
<node text="Store" tag="b" imageindex="1" />
<node text="City" tag="c" imageindex="2" />
</node>
</Fruits>
Expected Result:
<?xml version="1.0" encoding="utf-8"?>
<Fruits>
<node text="Apple" tag="a" imageindex="0">
<node text="Store" tag="b" imageindex="1" />
<node text="price" tag="b" imageindex="1" />
<node text="qty" tag="b" imageindex="1" />
<node text="amount" tag="b" imageindex="1" />
<node text="City" tag="c" imageindex="2" />
</node>
<node text="Orange" tag="a" imageindex="0">
<node text="Store" tag="b" imageindex="1" />
<node text="City" tag="c" imageindex="2" />
</node>
</Fruits>
Here's what I have done so far. I am having difficulty defining the detail path using the attribute. I know Linq to XML has a straight forward approach but couldn't figure out. Though I tried foreach but no avail, it gets more complicated at least to me.
XElement xFruit = XElement.Load(@"D:\Xml\Fruit.xml");
XElement detail = xFruit...
detail.Add(
new XElement("price", "$10"),
new XElement("qty", "10"),
new XElement("amount", "$100")
);
xFruit.Save(@"D:\Xml\Fruit.xml");
Any help would be greatly appreciated!
