Looking to add an attribute to an existing xml element <D_COMMS>
, not replace the existing attribute just add it to the beginning.
This is the XML
<OUTPUT version="2.0">
<RESPONSE>
<DATA id="17fb13cca6c5463597fdf340c044069f">
<![CDATA[<ID> jdfkldklfjdkl</ID><D_COMMS>ON this date...</D_COMMS>]]>
</DATA>
</RESPONSE>
This XML is the result of a HTTPWebResponse so this is what the XMl looks like when it comes back to me and I need to add a value to the D_COMMS element and send it back.Tried something like this to look for the descendant DATA and add it that way.
var addelement = doc.Descendants("DATA").First();
addelement.Add(XElement("D_COMMS","On this date we said"));
Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always a name-value pair.
Add a new attribute to the element, using setAttribute(String name, String value) . Call void prettyPrint(Document xml) method of the example. The method gets the xml Document and converts it into a formatted xml String, after transforming it with specific parameters, such as encoding.
XML elements can have attributes, just like HTML. Attributes are designed to contain data related to a specific element.
The root element of an XML message is based on an object structure and an operation specified for the channel or service used for the communication. The root element can contain one or more attributes. The following table shows the attributes that can apply to root elements.
A better one to set attribute is in here Adding attributes to an XML node
XmlElement id = doc.CreateElement("id");
id.SetAttribute("userName", "Tushar");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With