Today's post will show a way to add new element to the existing XML Document with list of attributes name as contained in other elements with the help of LINQ in C#.
Herein the sample customers.xml file used for the current example
Customers.xml
//retreiving all the elements from the xDoc object
Herein the sample customers.xml file used for the current example
Customers.xml
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
<customer name="Steve John" Id="CUST0001" location="New York "
country="United States " DOB="12/23/1960" Phone="+12126661111"/>
country="
<customer name="Sagar Mehta" Id="CUST0106" location="Bangalore "
country="India " DOB="07/23/1978" Phone="+918088881001" />
country="
<customer name="Rahul Raj" Id="CUST0190" location="Delhi"
country="India" DOB="01/31/1980" Phone="+911166661111" />
country="India" DOB="01/31/1980" Phone="+911166661111" />
<customer name="John Roderix" Id="CUST0301" location="Sydney"
country="Australia" DOB="02/13/1956" Phone="+61(0)280623999"/>
country="Australia" DOB="02/13/1956" Phone="+61(0)280623999"/>
</Customers>
//Loading the customers xml into the XDocument object
XDocument xDoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory+ "/Customers.xml");
There could be different ways to add the same attributes details as exists in other elements.
Scenario 1: Adding the New element with Attributes details using Attributes name in the XElement creation.
Herein a new element is created with the existing set of attributes name.
Now newly created element "oXElementNew" will be added to the existing xDoc object of XML document.
xDoc.Root.Add( oXElementNew );
Now all the Element details from the XML document will be retrieved to see whether the newly added Element is added to the xDoc object.
Scenario 1: Adding the New element with Attributes details using Attributes name in the XElement creation.
Herein a new element is created with the existing set of attributes name.
XElement oXElementNew =
new
XElement("Customer",
new XAttribute("name", "Martin
King"),
new XAttribute("city", "New
York"),
new XAttribute("country", "USA"),
new XAttribute("emailid", "martinking@martin.com"));
Now newly created element "oXElementNew" will be added to the existing xDoc object of XML document.
xDoc.Root.Add( oXElementNew );
Now all the Element details from the XML document will be retrieved to see whether the newly added Element is added to the xDoc object.
//retreiving all the elements from the xDoc object
IEnumerable<XElement>
rows =
from row in xDoc.Descendants("Customer")
select row;
//looping through all the elements
Scenario 2: First getting the count of Attributes in a element and then creating the New element with Attributes details using Attributes array index position.
//assigning the each attributes index a value
foreach (XElement
xe in rows)
{
//retreiving all the attributes from the current xElement
IEnumerable<XAttribute> attList =
from
att in
xe.DescendantsAndSelf().Attributes()
select
att;
//looping through the attributes collection
foreach (XAttribute
xAtt in attList)
{
//displaying the individual attributes details for the
//selected element
Console.WriteLine(xAtt);
}
//to display a separator between two customer records
Console.WriteLine("---Customer details separator---");
}
The newly created element details can be seen in the output window.Scenario 2: First getting the count of Attributes in a element and then creating the New element with Attributes details using Attributes array index position.
//getting the count of Attributes from
an Element of XML Document
int attributesCount = xDoc.Descendants("Customer").ElementAt(0).Attributes().Count();
//Definiting an array varible of type
XAttribute and defining the array size using the //attributesCount
XAttribute[] xAttribute = new
XAttribute[attributesCount];
//assigning the each attributes index a value
xAttribute[0] = new XAttribute("name","Rahul Pradhan");
xAttribute[1] = new XAttribute("city",
"Hyderabad");
xAttribute[2] = new XAttribute("country",
"India");
xAttribute[3] = new XAttribute("emailid",
"rahul.p@gmail.com");
//creating the new xElement by passing the attribute
details
XElement oXElementNew = new
XElement("Customer",
xAttribute);
//adding the newly created element to the Xml root node
xDoc.Root.Add(oXElementNew);
//now we will retrieve all the elements from the xDoc object
IEnumerable<XElement> rows =
from row in xDoc.Descendants("Customer")
select row;
//looping through all the elements
foreach (XElement xe in rows)
{
//retreiving all the attributes from the current xElement
IEnumerable<XAttribute> attList =
from att in
xe.DescendantsAndSelf().Attributes()
select att;
//looping through the attributes collection
foreach (XAttribute xAtt in attList)
{
//displaying the individual attributes details for the
//selected element
Console.WriteLine(xAtt);
}
//to display a separator between two customer records
Console.WriteLine("---Customer details separator---");
}
The newly created element details can be seen in the output window.
The newly created element details can be seen in the output window.
I have used Console.Write for writing the output to demonstrate the output retrieved. You can use any way the data is required to be displayed or stored in a collection.
Happy Coding.........
Comments
Post a Comment