Adding XML Attribute To An Existing XML Node

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,

<p style=" I have requirement where I need to add an XML Attribute to an existing XML Node.
<p style=" Say for example I have an XML file as below :
<p style="
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; xml<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; User <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Naimish<span style="color:Black; " <span style="color:Red; Country<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; India<span style="color:Black; " <span style="color:Red; Mobile<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0123456789<span style="color:Black; " <span style="color:Red; UserExits<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Yes<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; User <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Vishal<span style="color:Black; " <span style="color:Red; Mobile<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0123456788<span style="color:Black; " <span style="color:Red; UserExits<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Yes<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; User <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Lucky<span style="color:Black; " <span style="color:Red; Country<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; India<span style="color:Black; " <span style="color:Red; Mobile<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0123456787<span style="color:Black; " <span style="color:Red; UserExits<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Yes<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; User <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Ravi<span style="color:Black; " <span style="color:Red; Mobile<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0123456786<span style="color:Black; " <span style="color:Red; UserExits<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Yes<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; User <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; XYZ<span style="color:Black; " <span style="color:Red; Country<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; India<span style="color:Black; " <span style="color:Red; Mobile<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0123456785<span style="color:Black; " <span style="color:Red; UserExits<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Yes<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xml<span style="color:Blue; >
[/code]

<p style=" <br/>
Now, If you notice, <strong style="padding:0px; margin:0px User Vishal and <strong style="padding:0px; margin:0px User Ravi donât have <strong style="padding:0px; margin:0px an attribute Country ,
so I have added below code :
<p style="
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; class AddAttributesToXML
{
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
<span style="color:Blue; if (File.Exists(<span style="color:#A31515; "C:\Test.xml"))
{
XmlDocument XDoc = <span style="color:Blue; new XmlDocument();
XDoc.Load(<span style="color:#A31515; "C:\Test.xml");
XDoc.InnerXml = AddAttributesToXML(XDoc.OuterXml);
XDoc.Save(<span style="color:#A31515; "C:\Test.xml");
}
}

<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; string AddAttributesToXML(String inputXml)
{
XmlDocument xmlDoc = <span style="color:Blue; new XmlDocument();
xmlDoc.LoadXml(inputXml);

XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName(<span style="color:#A31515; "User");

<span style="color:Blue; foreach (XmlNode xmlNode <span style="color:Blue; in xmlNodeList)
{
<span style="color:Blue; try
{
<span style="color:Blue; if (xmlNode.Attributes.GetNamedItem(<span style="color:#A31515; "Country").Value == <span style="color:Blue; null) { }
}
<span style="color:Blue; catch (Exception)
{
XmlAttribute CountryAttr = xmlDoc.CreateAttribute(<span style="color:#A31515; "Country");
CountryAttr.Value = <span style="color:#A31515; "USA";
xmlNode.Attributes.Append(CountryAttr);
}
}

<span style="color:Blue; return xmlDoc.OuterXml;
}
}
[/code]

<p style=" <br/>
This works just fine!!
<p style=" But I suppose using LINQ To XML, I can implement the same with better approch??!!
<p style=" Thanks

<p style="
<p style=" <hr class="sig https://twitter.com/#!/NaimishPandya @NaimishPandya | mailto:pandya.naimish@hotmail.com
Email | http://www.linkedin.com/in/naimishpandya LinkedIn | http://naimishpandya.wordpress.com/
My Blog

View the full article
 
Back
Top