Querying XML document - c#

I am trying to query an xml document, but this code doesn't read xml parts with closed tag notation but reads fine xelement. Can anyone spot what I'm doing wrong?
I have program generated XML document which gives closed tagged file hence its an issue now..
<?xml version="1.0" encoding="utf-8" ?>
<Student>
<Person name="John" city="Auckland" country="NZ" />
<Person>
<Course>GDICT-CN</Course>
<Level>7</Level>
<Credit>120</Credit>
<Date>129971035565221298</Date>
</Person>
<Person>
<Course>GDICT-CN</Course>
<Level>7</Level>
<Credit>120</Credit>
<Date>129971036040828501</Date>
</Person>
</Student>
class Program
{
static void Main(string[] args)
{
XDocument xDoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "Customers.xml");
IEnumerable<XElement> rows = from row in xDoc.Descendants("Person") select row;
foreach(XElement xEle in rows)
{
IEnumerable<XAttribute>attlist = from att in xEle.DescendantsAndSelf().Attributes() select att;
foreach(XAttribute xatt in attlist)
{
Console.WriteLine(xatt);
}
Console.WriteLine("-------------------------------------------");
}
Console.ReadLine();
}
}

Since you have added Course and other attributes as XElement, so you need to loop on XElements instead of attributes -
foreach (XElement xEle in rows)
{
IEnumerable<XAttribute> attlist = from att in xEle.DescendantsAndSelf()
.Attributes() select att;
foreach (XAttribute xatt in attlist)
{
Console.WriteLine(xatt);
}
foreach (XElement elemnt in xEle.Descendants())
{
Console.WriteLine(elemnt.Value);
}
Console.WriteLine("-------------------------------------------");
}

First you must navigate to Person level and iterate through Persons, then for each Person you can iterate through its attributes.
private static void Main(string[] args)
{
//string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//XDocument xDoc = XDocument.Load(path + "\\Student Data\\data.xml");
XDocument xDoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "Customers.xml");
IEnumerable<XElement> xElements = xDoc.Descendants("Person");
foreach (XElement element in xElements)
{
foreach (XAttribute attribute in element.Attributes())
{
Console.WriteLine(attribute);
}
Console.WriteLine("-------------------------------------------");
}
Console.ReadLine();
}

Related

How to update xml file without using Linq?

I have this xml file:
<students>
<student>
<name>Mark</name>
<age>20</age>
</student>
<student>
<name>Adam</name>
<age>32</age>
</student>
I want to change the age of Adam, for that I did:
XmlDocument doc = new XmlDocument();
doc.Load("/path/to/sutdents.xml");
XmlNode n = doc.SelectSingleNode("/students/student[name='" + student.name +
"']");
if (n != null)
{
n.SelectNodes("age").Item(0).FirstChild.Value = new_value;
}
but I get "n" as a null value.
I tried another way to do that by searching for the node using foreach and make change, but I get Null Exception:
XmlDocument doc = new XmlDocument();
doc.Load("/path/to/sutdents.xml");
XmlNodeList nodes = doc.SelectNodes("/students/student");
foreach (XmlNode node in nodes)
{
if (node.FirstChild.InnerText == student.name)
{
node.SelectSingleNode("age").InnerText = new_value;
}
}
what I missing here please?
Thanks in advance
I tried to run your code. that is work! check what is the value of student.name.
XML [sutdents.xml]
<?xml version="1.0" standalone="yes"?>
<students>
<student>
<name>Mark</name>
<age>20</age>
</student>
<student>
<name>Adam</name>
<age>32</age>
</student>
</students>
sample code:
XmlDocument doc = new XmlDocument();
doc.Load("sutdents.xml");
XmlNode n = doc.SelectSingleNode("/students/student[name='Adam']");
if (n != null)
{
n.SelectNodes("age").Item(0).FirstChild.Value = "44";
}
doc.Save("sutdents.xml");
i hope this helps;
When Using foreach:
XmlNodeList studentsList = doc.ChildNodes[0].SelectNodes("student");
foreach (XmlNode node in studentsList)
{
if (node.ChildNodes[0].InnerText == student.name) //name
{
node.ChildNodes[1].InnerText = new_value; //age
}
}
Your Code is Correct here is a screen shot of your debugged code with same xml and mentioned code, check you are passing student.name with correct value and case sensitive.
Try this using XDocument:
private static void UpdateXML(string xmlpath, string studentName, int age)
{
var doc = XDocument.Load(xmlpath);
//Get student
var student = doc.Descendants("student").Where(att => att.Element("name") != null && att.Element("name").Value.Equals(studentName)).FirstOrDefault();
if (student != null)
student.Element("age").Value = age.ToString();
doc.Save(xmlpath);
}

How to check a string exists in a xml node's value asp.net

My xml will be like below
<Employee>
<Emp>
<Name id="1" link="/office1/manager"></Name>
<Name id="2" link="/office/sweeper"></Name>
<Name id="3" link="/office2/manager"></Name>
</Emp>
</Employee>
I want to get the "id" of the employees who contains string "manager" in "link"
Using linq to xml:
XDocument doc = XDocument.Load("XMLFilePath");
var selectors = from elements in doc.Elements("Employee").Elements("Emp").Elements("Name")
where elements.Attribute("link").Value.Contains("manager")
select elements;
string ids = string.Empty;
foreach (var element in selectors)
{
ids += element.Attribute("id").Value + ",";
}
Also, for loading from string you can use:
XDocument doc = XDocument.Parse(xmlString);
var xDoc = XDocument.Parse(xml); //XDocument.Load(filename)
var ids = xDoc.Descendants("Name")
.Where(n => n.Attribute("link").Value.Contains("/manager"))
.Select(n => n.Attribute("id").Value)
.ToList();
//SELECT THE VALUES FROM XML USING C#
<?xml version="1.0" encoding="iso-8859-1"?>
<CONFIG>
<UsersList>
<SystemName>DOTNET-PC</SystemName>
<UserName>KARTHIKEYAN</UserName>
<ImagePath>C:\Users\DEVELOPER\AppData\Roaming\Office Messenger\assets\insta.jpg</ImagePath>
<PhotoPath>C:\Users\DEVELOPER\AppData\Roaming\Office Messenger\assets\NoPhoto.png</PhotoPath>
<UserStatus>Available</UserStatus>
<CustomStatus>Available</CustomStatus>
<theme>FF8B0000</theme>
</UsersList>
</CONFIG> //XML DOCUMENT
//C#
DataSet ds = new DataSet();
try
{
ds.ReadXml("C:\\config.xml");
}
catch { };
if (ds.Tables.Count > 0)
{
var results = from myRow in ds.Tables[0].AsEnumerable() where myRow.Field<string> ("SystemName") == SystemInformation.ComputerName select myRow;//ds.Tables[0] is <CONFIG> tag //in where SystemName=My system name to select the values from xml
foreach (var cust in results)
{
string _myName = cust["UserName"].ToString();
string _myLogoPath = cust["ImagePath"].ToString();
string _customStatus = cust["CustomStatus"].ToString();
string _myPhotoPath = cust["PhotoPath"].ToString();
}
}
//CREATE XML FROM C#
XDocument xmlDoc = XDocument.Load("C:\\config.xml");
xmlDoc.Root.Add(new XElement("UsersList", new XElement("SystemName", SystemInformation.ComputerName), new XElement("UserName", SystemInformation.ComputerName), new XElement("ImagePath", _filesPath + "\\insta.jpg"), new XElement("PhotoPath", _filesPath + "\\NoPhoto.png"), new XElement("UserStatus", "Available"), new XElement("CustomStatus", "Available"), new XElement("theme", "000000")));
xmlDoc.Save("C:\\config.xml");

How to delete selected node from XML in C#?

I am New To C# I have problem...
I want to delete selected node from My XMl File
Here I just tried with this code but I didn't get please can any one help in that
private void btnDelete_Click(object sender, EventArgs e)
{
xdoc.Load(strFilename);
string Xpath = string.Format("root/{0}/{1}",_strProCat,_strProdType);
xdoc.SelectSingleNode(Xpath).RemoveAll();
xdoc.Save(strFilename);
MessageBox.Show("Deleted Successfully");
}
Here My Xml File
<root>
<product category="Soaps">
<product type="Washing">
<product name="Rin">
<Id>100</Id>
<AvailProducts>30</AvailProducts>
<Cost>20.00</Cost>
</product>
<product name="Tide">
<Id>101</Id>
<AvailProducts>30</AvailProducts>
<Cost>15.00</Cost>
</product>
</product>
</product>
</root>
Just I want to delete Node which product name="Tide"
You can simple use the below code:
private void btnDelete_Click(object sender, EventArgs e)
{
var xDoc = XDocument.Load(strFilename);
foreach (var elem in xDoc.Document.Descendants("product"))
{
foreach (var attr in elem.Attributes("name"))
{
if (attr.Value.Equals("Tide"))
elem.RemoveAll();
}
}
xDoc.Save(destinationFilename);
MessageBox.Show("Deleted Successfully");
}
Happy Coding...
Something like this should do it:
xdoc.Elements("product").Where(x=> x.Element("name").Value == "Tide").FirstOrDefault().Remove();
If you want XPath with XmlDocument then following is the way to do it..
XmlDocument xdoc = new XmlDocument();
xdoc.Load(strFilename);
string Xpath = string.Format("root/product[#category='{0}']/product[#type='{1}']/product[#name='{2}']", "Soaps", "Washing", "Tide");
xdoc.SelectSingleNode(Xpath).RemoveAll();
xdoc.Save(strFilename);
Update
As per your Requirement To Remove the empty node, try following code to remove empty node as
XmlNodeList emptyElements = xdoc.SelectNodes(#"//*[not(node())]");
for (int i = emptyElements.Count - 1; i > -1; i--)
{
XmlNode nodeToBeRemoved = emptyElements[i];
nodeToBeRemoved.ParentNode.RemoveChild(nodeToBeRemoved);
}
Now your final full flesh code will look like as
string Xpath = string.Format("root/product[#category='{0}']/product[#type='{1}']/product[#name='{2}']", "Soaps", "Washing", "Tide");
xdoc.SelectSingleNode(Xpath).RemoveAll();
XmlNodeList emptyElements = xdoc.SelectNodes(#"//*[not(node())]");
for (int i = emptyElements.Count - 1; i > -1; i--)
{
XmlNode nodeToBeRemoved = emptyElements[i];
nodeToBeRemoved.ParentNode.RemoveChild(nodeToBeRemoved);
}
xdoc.Save(strFilename);

C# : Modify a xml node

i have that xml file :
<?xml version="1.0" encoding="utf-8"?>
<reminders>
<reminder>
<Title>Alarm1</Title>
<Description>Desc1</Description>
<Time>03/07/2012 10:11AM</Time>
<snooze>1</snooze>
<repeat>None</repeat>
</reminder>
</reminders>
And i want to modify the innertext from Alarm1 to another value so i wrote that code which actually duplicate the entire node .
XmlDocument xml = new XmlDocument();
xml.Load("0.xml");
XmlNodeList elements = xml.SelectNodes("//reminders");
foreach (XmlNode element in elements)
{
if (element.InnerText == "Alarm1")
{
XmlNode newvalue = xml.CreateElement("MODIFIED");
element.ReplaceChild(newvalue, element);
xml.Save("0.xml");
}
}
And then tried another code :
foreach (XmlElement element in xml.SelectNodes("//reminder"))
{
if (element.InnerText == "Alarm1")
{
XmlNode newvalue = xml.CreateElement("MODIFIED");
element.ReplaceChild(newvalue, element);
xml.Save("0.xml");
}
}
But also doesn`t work....
EDIT 1 : [Figured out a new code]
XmlDocument xml = new XmlDocument();
xml.Load("0.xml");
foreach (XmlElement element in xml.SelectNodes("//reminder"))
{
foreach (XmlElement element1 in element)
{
if (element.SelectSingleNode("//Title").InnerText == "Alarm1")
{
XmlNode newvalue = xml.CreateElement("MODIFIED");
element.ReplaceChild(newvalue, element1);
xml.Save("0.xml");
}
}
}
But it made the Alarm1 becomes
<MODIFIED />
EDIT 2 : [I SOLVED IT :D]
Okay here is the code i could figure out :
XmlDocument xml = new XmlDocument();
xml.Load("0.xml");
foreach (XmlElement element in xml.SelectNodes("//reminder"))
{
foreach (XmlElement element1 in element)
{
if (element.SelectSingleNode("//Title").InnerText == "Alarm1")
{
MessageBox.Show(element1.InnerText);
XmlNode newvalue = xml.CreateElement("Title");
newvalue.InnerText = "MODIFIED";
element.ReplaceChild(newvalue, element1);
xml.Save("0.xml");
}
}
}
I`ll really appreciate your helps and thanks.
Try this:
xml.SelectSingleNode("//reminder/Title").InnerText = "NewValue";
Your foreach line is simply looping through a list of elements called "reminders", not it's child nodes.
Take a look at this xpath tutorial for more information:
http://www.w3schools.com/xpath/xpath_intro.asp
If you want to use linq with xml (I find it the best way) then you will want to use the System.Xml.Linq namespace. The classes in that namespace are all prefixed with just X not Xml. The functionality in this namespace is newer, better and much easier to manipulate with Linq.
var xml = XDocument.Load("0.xml");
var alarm1 = xml.Descendants("reminder")
.Single(r => r.Element("Title") == "Alarm1");
This code will give you a variable, alarm1 that is the reminder that has a title node of "Alarm1."
From that point its not clear to me exactly what you want to modify. If you just want to change the title then ...
alarm1.Element("Title").Value = "MODIFIED";
xml.Save("0.xml");
XDocument doc = XDocument.Load("0.xml");
IEnumerable<XElement> rech =
from el in doc.Root.Elements("reminder")
where (string)el.Element("Title") == "Alarm1"
select el;
if (rech.Count() != 0)
{
foreach (XElement el in rech)
{
el.Element("Title").SetValue("NEW TITLE");
}
}
doc.Save("0.xml");
XDocument xDoc = XDocument.Load(.....);
xDoc.Descendants("Title").First().Value = "New Value";
xDoc.Save(...)
XmlDocument xml = new XmlDocument();
xml.Load(...);
var newTitle = "MODIFIED";
var title_node = xml.GetElementsByTagName("Title");
if(!string.IsNullOrEmpty(newTitle) && title_node.Count > 0)
{
title_node[0].InnerText = newTitle;
}

Looping through an XML document and assigning data in C# variables

I have an XML file that has several tags like this:
<sitecollection name="">
<site name="">
<maingroup name="">
<group name=""> </group>
</maingroup>
</site>
<sitecollection>
The idea is to loop through all the sitecollection and it's child elements in the XML document, and save the info in variables. The problem I'm having is saving the child elements, with their attributes.
So far I have the following code:
class xmlreader
{
public static void Main()
{
XDocument xdoc = XDocument.Load("xmldocument.xml");
var result = new System.Text.StringBuilder();
var lv1s = from lv1 in xdoc.Descendants("sitecollection")
select new
{
siecollection = lv1.Attribute("name").Value,
maingroup = lv1.Descendants("group")
};
foreach (var lv1 in lv1s)
{
result.AppendLine(lv1.siecollection);
foreach (var lv2 in lv1.maingroup)
result.AppendLine(" " + lv2.Attribute("name").Value);
}
}
}
If the sample XML you provided is accurate, the problem is likely that you don't have a closing tag for sitecollection.
I tried your code with a slightly varied XML input (closed the sitecollection tag, and added some values to the name attributes so there would be something to collect in the result StringBuilder) as follows:
XDocument xdoc = XDocument.Parse(#"<sitecollection name=""collectionName"">
<site name=""sitename"">
<maingroup name=""maingroupname"">
<group name=""groupname""> </group>
</maingroup>
</site>
</sitecollection>
");
and result.ToString() produces: "collectionName\r\n groupname\r\n"
Take this where you will:
XmlReader reader = XmlReader.Create(#"C:\file.xml", null);
StringBuilder result = new StringBuilder();
while (reader.Read())
{
if( reader.NodeType == XmlNodeType.Element)
{
if (reader.HasAttributes)
{
result.AppendLine(reader.LocalName);
reader.MoveToFirstAttribute();
result.AppendLine(" " + reader.Value);
}
}
}

Categories