0

My XML:

<Bank>
    <Customer id="0">
        <Accounts>
            <Account id="0" />
            <Account id="1" />
        </Accounts>
    </Customer>
    <Customer id="2">
        <Accounts>
            <Account id="0" />
        </Accounts>
    </Customer>
</Bank>

i want to add new account element before customer id=2. i have this xml in xelement and i want to add other xelement to the first. how can do that? Thanks for the help.

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
Ammar.Dev
  • 3
  • 1
  • 4

1 Answers1

1

linq-to-xml makes this easy:

// Parse our XML document to an XDocument
var xml = @"<Bank>
    <Customer id=""0"">
        <Accounts>
            <Account id=""0"" />
            <Account id=""1"" />
        </Accounts>
    </Customer>
    <Customer id=""2"">
        <Accounts>
            <Account id=""0"" />
        </Accounts>
    </Customer>
</Bank>";
var doc = XDocument.Parse(xml);

// Create our new Customer to add
var newCustomer = new XElement("Customer",
    new XAttribute("id", "1"),
    new XElement("Accounts",
        new XElement("Account", new XAttribute("id", "0"))
    )
);

// Find the customer with id="2"
var customer2 = doc.Root.Elements("Customer").First(x => x.Attribute("id").Value == "2");
// Add the new customer before the customer with id="2"
customer2.AddBeforeSelf(newCustomer);
canton7
  • 37,633
  • 3
  • 64
  • 77
  • is it necessary to use the doc=Xdocument.parse(xml)? because i used a XElement XElement myxElement = new XElement(); MemoryStream myStream = new MemoryStream(); myxElement.Save(myStream); isn't possible to update this myxElement ? – Ammar.Dev Feb 07 '20 at 10:38
  • @Ammar.Dev You can get the `XDocument` however you like. I added it to my answer so you can just run it. In the code you just posted, that's you *saving* an XElement to a stream, not *loading* it from somewhere – canton7 Feb 07 '20 at 10:46
  • @ canton7 no i mean i loaded the xdocument and i want to save that as XElement but before save that i want to add to this XElement an Other XElement. and that's this title of my post. i mean how to add a new XElement to an Exist XElement ? – Ammar.Dev Feb 07 '20 at 10:58
  • @Ammar.Dev So you've already got the code to create `doc`? If so, skip the first two lines in my answer, and start directly at the bit which creates `newCustomer` – canton7 Feb 07 '20 at 10:59
  • @ canton7 and what about doc.Root ? i don't know my doc because i just have XElement? – Ammar.Dev Feb 07 '20 at 11:02
  • @Ammar.Dev If you've got an `XElement` called `element`, then do `element.Elements("Customer")...` (i.e. just remove `.Root`). I would recommend parsing your XML document to an `XDocument` rather than an `XElement` though, as `XDocument` has additional features which relate specifically to documents. – canton7 Feb 07 '20 at 11:03
  • @ canton7 is it possible to parse an XElement to Xdocument ?if is it possible it will be great and i can fix that – Ammar.Dev Feb 07 '20 at 11:06
  • No. Like I said, `XDocument` contains things which `XElement` doesn't. Where you currently create your root `XElement`, just change that to instead create an `XDocument`. Regardless, the code in my answer will work if you just have an `XElement`: just remove `.Root`, as I said before – canton7 Feb 07 '20 at 11:07