Question

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>
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top