Using XPath to read xml - c#

I have this XML:
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book id="ISBN-NUMBER">
<title>
The Autobiography of Benjamin Franklin
</title>
<author type="major">
<first-name>
Benjamin
</first-name>
<last-name>
Franklin
</last-name>
</author>
<price>
8.99
</price>
</book>
<book id="ISBN-NUMBER">
<title>
The Confidence Man
</title>
<author type="major">
<first-name>
Herman
</first-name>
<last-name>
Melville
</last-name>
</author>
<price>
11.99
</price>
</book>
<book id="ISBN-NUMBER">
<title>
The Gorgias
</title>
<author type="major">
<name>
Plato
</name>
</author>
<price>
9.99
</price>
</book>
</bookstore>
How do i read it with XPath? I've used:
XPathDocument doc = new XPathDocument(stream);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator node = nav.Select("bookstore/book");
while (node.MoveNext())
but how do i move on from here? i guess i need a swtich statement to swtich if node is title, author, and price. if book then i need to read books id, same with author and type.

You can use node.Select('//title') to get the title, I suppose. I'm not sure how this works in C#, but common xpaths are as follows
//bokstore/book selects book nodes.
//bookstore/book/title selects the title node
//bookstore/book[n]/* selects all the all the child nodes of nth node.

Related

How to remove the parent node and xml declaration in a xml file using C#?

I wish to remove the Parent node and xml declaration from the file. Below is the input file.
<?xml version="1.0" encoding="utf-8"?>
<Items>
<Product>
<ID>001</ID>
<Name>John</Name>
<Designation>Developer</Designation>
</Product>
</Items>
I need to remove the XML declaration and parent node . Like below.
<Product>
<ID>001</ID>
<Name>John</Name>
<Designation>Developer</Designation>
</Product>
How could I do it using C#.NET?
You simply parse the XML with XDocument, like this:
var xml = #"<?xml version=""1.0"" encoding=""utf-8""?>
<Items>
<Product>
<ID>001</ID>
<Name>John</Name>
<Designation>Developer</Designation>
</Product>
</Items>";
var doc = XDocument.Parse(xml);
var s = doc.Root.Element("Product").ToString();
Console.WriteLine(s);
Which outputs
<Product>
<ID>001</ID>
<Name>John</Name>
<Designation>Developer</Designation>
</Product>

Linq XML add new parent

With linq XML is it possible to add a new father to existing nodes?
Take this XML excerpt:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<items>
<book>
<title>Title 1</title>
<author>Author 1</author>
</book>
<book>
<title>Title 2</title>
<author>Author 2</author>
</book>
<car>
<model>Tesla</model>
</car>
</items>
Is it possible to add a new father "books" to book like this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<items>
<books>
<book>
<title>Title 1</title>
<author>Author 1</author>
</book>
<book>
<title>Title 2</title>
<author>Author 2</author>
</book>
</books>
<car>
<model>Tesla</model>
</car>
</items>
This is not working because it is cloning the nodes:
doc.Element("items").Add(new XElement("books",doc.Element("items").Elements("book")));
You can remove your existed <book> elements from <items> node after adding them under new <books> parent node:
var books = doc.Element("items").Elements("book");
doc.Element("items").Add(new XElement("books", books));
books.Remove();

How to make an xml file in c# for use with .iqy which imports as a datatable

I want to make a excel file where the data is collected and updated automatically off my site and display as data tables.
I have tried to web import off the site from a table I have created but it is not formatted as a data table.
I have also created a xml file and used a .iqy file to get the data and still it is not imported as a data table
This is my .iqy file which will import data but not as a xml
WEB
1
http://localhost:55369/files/SerializationOverview.xml
This is my test xml file
<?xml version="1.0"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Thank you in advance any help is much appreciated.
Read the third/last line of your .iqy file:
var url = File.ReadLines(#"query.iqy").Take(3).Last();
Then download the xml like using:
using (var wc = new System.Net.WebClient())
{
contents = wc.DownloadString(url);
}
And then parse the xml using XDocument.Parse(contents), go through the xml and write the data to the database.

What am i doing wrong in this LINQ statement?

I am studying Linq to xml and i am reading a xml file using linq.. I used the following method,
public void getbooklist(string Path)
{
XDocument xdoc = XDocument.Load(Path);
var books = new book in xdoc.Elements("book")
select book;
}
and my xml file looks like this,
<?xml version="1.0" encoding="utf-8" ?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>
A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
</description>
</book>
</catalog>
and when i tried to execute the method i get an error
A new expression requires (), [], or {} after type... What am i doing wrong?
You code should be something like this:
XDocument xdoc = XDocument.Load(Path);
var books = from b in xdoc.Elements("book")
select b;

Removing preprocessing instructions from an XML in c#

How can i remove preprocessing instructions from an XML in a greener way? suppose that I have this xml in a string variable (which is a property of a class),I wanted to write it as value of another xml node,How can i achieve it in a cleaner way?
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book genre="autobiography">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>
You help would be much appreciated !!
If you can load it in an XmlDocument: FirstChild returns the <?xml version="1.0" encoding="utf-8" ?> node, and NextSibling returns the rest.
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode node = doc.FirstChild.NextSibling;
Edit: Your xml looks a lot like the example on msdn for XPathNavigator.Select. Have you tried using that?
Edit2: You can get the name of the top level element using:
string topLevelNode = doc.DocumentElement.Name;
Try the code below. It will load the text to XmlDocument object first
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlAsText);
Then you can get the OutherXml using below:
xmlDoc.DocumentElement.OuterXml;

Categories

Resources