XPathNodeIterator access child nodes from iterated data - c#

<?xml version="1.0"?>
-<bookstore>
<book >
<title>aaaa</title>
-<author >
<first-name>firts</first-name>
<last-name>last</last-name>
</author>
<price>8.23</price>
<otherbooks>
<book >
<title>bbb</title>
<price>18.23</price>
</book>
<book >
<title>ccc</title>
<price>11.22</price>
</book>
</otherbooks>
</book>
</bookstore>
I have selected all books form xml file. How to select title, author( first and last name ) and price for each book with use of XPath?
xPathDoc = new XPathDocument(filePath);
xPathNavigator = xPathDoc.CreateNavigator();
XPathNodeIterator xPathIterator = xPathNavigator.Select("/bookstore//book");
foreach (XPathNavigator book in xPathIterator)
{
??
}

Use SelectSingleNode() and Value:
XPathDocument xPathDoc = new XPathDocument(filePath);
XPathNavigator xPathNavigator = xPathDoc.CreateNavigator();
XPathNodeIterator xPathIterator = xPathNavigator.Select("/bookstore//book");
foreach (XPathNavigator book in xPathIterator)
{
XPathNavigator nav = book.SelectSingleNode("title");
string title = nav==null ? string.Empty : nav.Value;
nav = book.SelectSingleNode("author/first-name");
string authorFirstName = nav==null ? string.Empty : nav.Value;
nav = book.SelectSingleNode("author/last-name");
string authorLastName = nav==null ? string.Empty : nav.Value;
nav = book.SelectSingleNode("price");
string price = nav==null ? string.Empty : nav.Value;;
Console.WriteLine("{0} {1} {2} {3}", title, authorFirstName, authorLastName, price);
}

You can use LINQ2XML
XElement doc=XElement.Load("yourXML.xml");//loads your xml
var bookList=doc.Descendants().Elements("book").Select(
x=>//your book node
new{
title=x.Element("title").Value,
author=new //accessing your author node
{
firstName=x.Element("author").Element("first-name").Value,
lastName=x.Element("author").Element("last-name").Value
},
price=x.Element("price").Value
}
);
bookList now have all the elements you want
So, you can do this now
foreach(var book in bookList)
{
book.title;//contains title of the book
book.author.firstName;//contains firstname of that book's author
book.author.lastName;
}

I like the solution provided by Mimo but with a tiny change, creating an extension method to re-use part of the functionality:
public static class XPathNavigatorExtensions
{
public static string GetChildNodeValue(this XPathNavigator navigator, string nodePath)
{
XPathNavigator nav = navigator.SelectSingleNode(nodePath);
return nav == null ? string.Empty : nav.Value;
}
}
The resulting code will look like cleaner:
ICollection<Book> books = new List<Book>();
foreach (XPathNavigator node in iterator)
{
Book book = new Book() { Author = new Author() };
book.Title = node.GetChildNodeValue("title");
book.Author.FirstName = node.GetChildNodeValue("author/first-name");
book.Author.LastName = node.GetChildNodeValue("author/last-name");
book.Price = node.GetChildNodeValue("price");
books.Add(book);
}

Related

How do I get number of a XML nodes child in c# XmlReader?

this is my XML structure:
<classes>
<Base Name="node1">
<Book Name="child01" CoverArtName="C102.jpg" CoverBaseFolder="" Tooltip="" PluginBook=""/>
<Book Name="child02" CoverArtName="C102.jpg" CoverBaseFolder="" Tooltip="" PluginBook=""/>
<Book Name="child03" CoverArtName="C102.jpg" CoverBaseFolder="" Tooltip="" PluginBook=""/>
</Base >
<Base Name="node2">
<Book Name="child01" CoverArtName="C102.jpg" CoverBaseFolder="" Tooltip="" PluginBook=""/>
<Book Name="child02" CoverArtName="C102.jpg" CoverBaseFolder="" Tooltip="" PluginBook=""/>
</Base >
<Base Name="node3">
</Base >
</classes>
how can i get number of children of each node with xmlReader?
Update:
I read my XML with thes code:
List<Bases> base7 = new List<Bases>();
XmlReader xmlReader = XmlReader.Create("Books.xml");
while (xmlReader.Read())
{
if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Base"))
{
if (xmlReader.HasAttributes)
Console.WriteLine(xmlReader.GetAttribute("Name") + ": " + xmlReader.GetAttribute("CoverBaseFolder"));
//Base Name
base7.Add(new Bases() { BaseName = xmlReader.GetAttribute("Name"), Basefolder = xmlReader.GetAttribute("CoverBaseFolder") });
}
}
mainbox.ItemsSource = base7;
The output is a list item with name of node and number of child elements of same node.
This can be done easily by using LinqToXml:
var list = XElement.Load("test.xml")
.Elements("Base")
.Select(e => new
{
Name = e.Attribute("Name").Value,
Count = e.Elements().Count()
})
.ToList();
But if you want to use the XmlReader, for example, to work with xml that does not fit in memory, the code is much more cumbersome:
var bases = new List<Base>();
using (var xmlReader = XmlReader.Create("test.xml"))
{
while (xmlReader.Read())
{
if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Base"))
{
var name = xmlReader.GetAttribute("Name");
int count = 0;
using (var innerReader = xmlReader.ReadSubtree())
{
while (innerReader.Read())
{
if (innerReader.NodeType == XmlNodeType.Element && innerReader.Name == "Book")
count++;
}
}
bases.Add(new Base { Name = name, Count = count });
}
}
}
class Base
{
public string Name { get; set; }
public int Count { get; set; }
}
To count the child nodes is convenient to use the ReadSubtree method.
The XmlReader class has many useful methods. Use ReadToFollowing method allows to slightly reduce code.
var bases = new List<Base>();
using (var xmlReader = XmlReader.Create("test.xml"))
{
while (xmlReader.ReadToFollowing("Base"))
{
string name = xmlReader.GetAttribute("Name");
int count = 0;
using (var innerReader = xmlReader.ReadSubtree())
{
while (innerReader.ReadToFollowing("Book"))
count++;
}
bases.Add(new Base { Name = name, Count = count });
}
}

read xml document and update all fields c#

i'm trying to read xml file and update all it's value my xml was
<adf>
<prospect>
<requestdate>2015-10-29 07-38-22</requestdate>
<id sequence="1" source="admin.ss.com">admin.ss.com</id>
<vehicle interest="buy" status="">
<id sequence="1" source=""></id>
<year></year>
<make></make>
<model>camry</model>
<vin></vin>
<stock></stock>
<trim></trim>
</vehicle>
<customer>
<contact primarycontact="1">
<name part="first">Jessica</name>
<name part="last">Sonntag</name>
<email>js#test.com</email>
<phone type="phone" time="day">555-585-5555</phone>
<address>
<street line="1"></street>
<city></city>
<regioncode></regioncode>
<postalcode></postalcode>
<country></country>
</address>
</contact>
<comments>Vehicle Year: 2011 Comments: </comments>
</customer>
<provider>
<name part="full">ST</name>
<service> Engine Marketing</service>
<phone>1-866-572-3952</phone>
</provider>
</prospect>
</adf>
so i select node like below
var items = (from item in xmlDoc.Descendants("requestdate")
select item).ToList();
then i can update only requestdata tag value so do i have to repeat same for all tags or is there any good way to accomplish this.
Regards
There is an easy way to do this. This one is a hidden gem. Most people may not know this. This feature came in VS2013 and it's called "Paste XML as Classes."
Save your xml (Ex: MyXml.XML)
Create a new Console project
Open the Xml in Visual studio
Copy All contents of the xml (Ctl+A, Ctl + C)
Add a new class to your project. You can give any name you like.
Go to Edit>Paste Special>Paste XML as classes.
Add another class to your project. Then add below two methods to that class.
public static string Serialise<T>(T serialisableObject)
{
var doc = new XmlDocument();
using (var stream = new StringWriter())
{
var settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
XmlWriter xmlWriter = XmlWriter.Create(stream, settings);
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
var xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(xmlWriter, serialisableObject, ns);
doc.LoadXml(stream.ToString());
}
return doc.InnerXml;
}
public static T Deserialise<T>(string xml)
{
T list;
using (var reader = new StringReader(xml))
{
var serialiser = new XmlSerializer(typeof(T));
list = (T)serialiser.Deserialize(reader);
}
return list;
}
Then in your console applications Main method; add this.
var myObj = new adf();
myObj.prospect = new adfProspect();
myObj.prospect.customer = new adfProspectCustomer(){comments = "dgsrtetetete"};
//populate all fields.....
var xml = MySerializer.Serialise(myObj);
File.WriteAllText(#"C:\myNewXml.xml", xml);
That's it. Same way now you can deserialise an xml object in to your class.
Try the XmlSerializer class: https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx If you serialize/deserialize the xml then up dating is trivial.
If you wanted to change every phone number to "0123456789" you could do something like:
var xDoc = XDocument.Load("document.xml");
var results = from phone in xDoc.Descendants("phone") select phone;
foreach (XElement result in results)
{
element.SetValue("0123456789");
}
i have came up with solution with support two extension method i'm iterating all nodes and update.(since my xml is not too big or complicated this one would be a good solution)
with help of these two extension methods
public static void IterateThroughAllNodes(this XmlDocument doc, Action<XmlNode> elementVisitor)
{
if (doc != null && elementVisitor != null)
{
foreach (XmlNode node in doc.ChildNodes)
{
DoIterateNode(node, elementVisitor);
}
}
}
public static void IterateThrough(this XmlNodeList nodes, Action<XmlNode> elementVisitor)
{
if (nodes != null && elementVisitor != null)
{
foreach (XmlNode node in nodes)
{
DoIterateNode(node, elementVisitor);
}
}
}
private static void DoIterateNode(XmlNode node, Action<XmlNode> elementVisitor)
{
elementVisitor(node);
foreach (XmlNode childNode in node.ChildNodes)
{
DoIterateNode(childNode, elementVisitor);
}
}
then i can update my xml nodes as below
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/xmlmail.xml"));
var email = new XmlEmail();
doc.IterateThroughAllNodes(
delegate(XmlNode node)
{
if (node.Name.Equals("requestdate"))
node.InnerText= email.RequestDate.ToLongDateString();
if (node.Name.Equals("vehicle"))
{
XmlNodeList childs = node.ChildNodes;
childs.IterateThrough(delegate(XmlNode vnode)
{
if (vnode.Name.Equals("id"))
vnode.InnerText= email.VehicleId.ToString();
if (vnode.Name.Equals("year"))
vnode.InnerText= email.Year.ToString();
if (vnode.Name.Equals("make"))
vnode.InnerText= email.Make;
if (vnode.Name.Equals("model"))
vnode.InnerText= email.Model;
if (vnode.Name.Equals("vin"))
vnode.InnerText= email.Vin;
if (vnode.Name.Equals("trim"))
vnode.InnerText = email.Trim;
});
}
if (node.Name.Equals("customer"))
{
XmlNodeList childs = node.ChildNodes;
childs.IterateThrough(delegate(XmlNode vnode)
{
if (vnode.Attributes != null && (vnode.Name.Equals("name") && vnode.Attributes["part"].Value.Equals("first")))
vnode.InnerText= email.FirstName;
if (vnode.Attributes != null && (vnode.Name.Equals("name") && vnode.Attributes["part"].Value.Equals("last")))
vnode.InnerText= email.LastName;
if (vnode.Name.Equals("email"))
vnode.InnerText= email.Email;
if (vnode.Name.Equals("phone"))
vnode.InnerText= email.Phone;
if (vnode.Name.Equals("comments"))
vnode.InnerText= email.Comments;
if (vnode.Name.Equals("address"))
{
XmlNodeList addresschilds = vnode.ChildNodes;
addresschilds.IterateThrough(delegate(XmlNode anode)
{
if (anode.Name.Equals("street"))
anode.InnerText= email.Street;
if (anode.Name.Equals("city"))
anode.InnerText= email.City;
if (anode.Name.Equals("phone"))
anode.InnerText= email.Phone;
if (anode.Name.Equals("regioncode"))
anode.InnerText= email.RegionCode;
if (anode.Name.Equals("postalcode"))
anode.InnerText= email.Postalode;
if (anode.Name.Equals("country"))
anode.InnerText= email.Country;
});
}
});
}
});

Parsing this xml

I tried a lot of codes but nothing worked.
I have XML:
<books>
<book>
<title>first title</title>
<publisher>first publisher</publisher>
<description>first description</description>
<published>1410</published>
</book>
<book>
<title>second book</title>
<publisher>second publisher</publisher>
<description>second description</description>
<published>1914</published>
</book>
[another book]
[another book2]
</books>
And I want input like this:
first title | first publisher | first description | 1410
second title | second publisher | second descirpion | 1914
[another books]
"My" Code:
var xdoc = XDocument.Load(#"5.xml");
var entries = from e in xdoc.Descendants("book")
select new
{
Title = (string)e.Element("title"),
Description = (string)e.Element("description")
};
//I DON'T KNOW WHAT IT DO, I FOUND THIS BUT I DON'T KNOW WHAT NEXT
I can parse first book but i can't parse multiple. Sorry for language.
If you want to use an XDocument you may try the following:
using System;
using System.Xml.Linq;
class Program
{
static void Main()
{
var doc = XDocument.Load("5.xml");
var books = doc.Descendants("book");
foreach (var book in books)
{
string title = book.Element("title").Value;
string publisher = book.Element("publisher").Value;
string description = book.Element("description").Value;
string published = book.Element("published").Value;
Console.WriteLine("{0}\t{1}\t{2}\t{3}", title, publisher, description, published);
}
}
}
If on the other hand the XML you are trying to parse is very big and cannot fit into memory it is better to use an XmlReader which will allow you to process it record by record:
using System;
using System.Xml;
class Program
{
static void Main()
{
using (var reader = XmlReader.Create("5.xml"))
{
string title = null, publisher = null, description = null, published = null;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "book")
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}", title, publisher, description, published);
}
if (reader.NodeType == XmlNodeType.Element && reader.Name == "title")
{
title = reader.ReadInnerXml();
}
if (reader.NodeType == XmlNodeType.Element && reader.Name == "publisher")
{
publisher = reader.ReadInnerXml();
}
if (reader.NodeType == XmlNodeType.Element && reader.Name == "description")
{
description = reader.ReadInnerXml();
}
if (reader.NodeType == XmlNodeType.Element && reader.Name == "published")
{
published = reader.ReadInnerXml();
}
}
}
}
}
With this approach you can deal with arbitrary large XML files.
You can use this code to parse your XML
XDocument xDoc = XDocument.Load("5.xml");
var books = (from b in xDoc.Descendants("book")
select new
{
title = (string) b.Element("title"),
publisher = (string) b.Element("publisher"),
despription = (string) b.Element("description"),
published = (string) b.Element("published")
}).ToList();
foreach (var book in books)
{
Console.WriteLine("{0} | {1} | {2} |{3}",book.title,book.publisher,book.despription,book.published);
}

how to get xml nth element name in wpf

public partial class XML_3 : Window
{
public XML_3()
{
this.InitializeComponent();
XmlDocument doc = new XmlDocument();
doc.Load("D:/sample.xml");
XmlNodeList student_list = doc.GetElementsByTagName("Student");
foreach (XmlNode node in student_list)
{
XmlElement student = (XmlElement)node;
int element_count = student.ChildNodes.Count;
}
}
}
In above code.I can get the count of element except root element(Student). now the count is 3.
But i have to get 2ed element name(Kavi),it's attribute element name(ID) and it's child element name(FName,MName).
what should i do to get those stuff.
Please help me...
Use XDocument (why?):
var doc = XDocument.Parse(xml); // OR Load(...)
var nodeCount = doc.Elements().Count();
var secondNode = doc.Elements().Skip(1).First();
var studentName = secondNode.Name;
var studentId = secondNode.Attribute("ID").Value;
or (for your code):
var secondNode = student.ChildNodes[1] as XmlElement;
var studentName = secondNode.LocalName;
var studentId = secondNode.Attributes["ID"];
Added:
var secondNode = student.ChildNodes[1];
var fName =
secondNode.ChildNodes.Cast<XmlElement>().FirstOrDefault(x => x.LocalName == "FName").InnerText;
var mName =
secondNode.ChildNodes.Cast<XmlElement>().FirstOrDefault(x => x.LocalName == "MName").InnerText;
var studentId = secondNode.Attributes["ID"].Value;

Enumerating Descents using LINQ query

I am trying to parse the following XML files in to a list. Unfortunately it returns only one element
Sample XML
<Titles>
<Book Title ="Love Story" Author= "Erich Segal" Year = "1999"/>
<Book Title ="Code Complete" Author= "Steve McConnel" Year = "2004"/>
<Book Title ="Rework" Author = "Jaso Fried" Year = "2010"/>
<Book Title ="Delivering Happiness" Author= "Tony Hseigh" Year = "2011"/>
</Titles>
C# Code
public class BookInfo
{
public string Title { get; set; }
public string Author { get; set; }
public int Year { get; set; }
}
XDocument xmlDoc = XDocument.Load(strXMLPath);
var b = from device in xmlDoc.Descendants("Titles")
select new BookInfo
{
Title = device.Element("Book").Attribute("Title").Value,
Author = device.Element("Book").Attribute("Author").Value,
Year = int.Parse(device.Element("Book").Attribute("Year").Value)
};
books = b.ToList();
I suspect you actually want to be finding descendants called "Book" rather than "Titles":
XDocument xmlDoc = XDocument.Load(strXMLPath);
var b = from book in xmlDoc.Descendants("Book")
select new BookInfo
{
Title = (string) book.Attribute("Title"),
Author = (string) book.Attribute("Author"),
Year = (int) book.Attribute("Year")
};
var books = b.ToList();
Or in non-query expression syntax:
XDocument xmlDoc = XDocument.Load(strXMLPath);
var books = xmlDoc.Descendants("Book")
.Select(book => new BookInfo
{
Title = (string) book.Attribute("Title"),
Author = (string) book.Attribute("Author"),
Year = (int) book.Attribute("Year")
})
.ToList();
EDIT: If you want all elements descending from Titles (e.g. to exclude "Book" elements from elsewhere), you'd want:
XDocument xmlDoc = XDocument.Load(strXMLPath);
var books = xmlDoc.Descendants("Titles")
.Descendants("Book")
.Select(book => /* same as before */)

Categories