Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a attribute to Root node of my XmlDocument() in C#

I already have a XmlDocument object in C#. Let's say the InnerXml looks like this;

<MyResponse>
  <ResponseType>
    <Id>8825</Id>
  </ResponseType>
</MyResponse>

How can I add a attribute which has the name as uuid and the value as 781283721381, e.g uuid="781283721381". So my Xml output would look like the following;

<MyResponse uuid="781283721381">
  <ResponseType>
    <Id>8825</Id>
  </ResponseType>
</MyResponse>
like image 977
neildt Avatar asked Jan 23 '26 11:01

neildt


1 Answers

document.DocumentElement.Attributes.Append(document.CreateAttribute("uuid")).Value = "12345"

Yeah, XmlDocument is a bit hard to modify, that's part of the reason why XDocument was invented. But it's the easiest to use XML serialization and work on objects.

like image 171
fejesjoco Avatar answered Jan 25 '26 01:01

fejesjoco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!