What am i doing wrong in this LINQ statement? - c#

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;

Related

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();

Reading and Editing XML file

I have an XML file which contains employee details. How can i edit the existing employee names in that MXL file using C# .NET.
Here is the xml file.
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee id="1">
<Name>Employee 1</Name>
<Designation>SE </Designation>
<Qualification>MCA </Qualification>
</Employee>
<Employee id="2">
<Name>Employee 2</Name>
<Designation>SE </Designation>
<Qualification>MCA </Qualification>
</Employee>
<Employee id="3">
<Name>Employee 3</Name>
<Designation>SE </Designation>
<Qualification>MCA </Qualification>
</Employee>
</Employees>
How can i edit the employee names. I am new to xml. For example using Console Application
You can simple do that using Linq to Xml.
using System.Xml.Linq;
...
XDocument xDoc = XDocument.Load(#"Your xml file path goes here"); // or XDocument.Parse("Your xml string goes here");
xDoc.Root.Elements("Employee").First(xe => xe.Attribute("id").Value == "1").Element("Name").Value = "your value";
Here is a good reference for head start: Programming Guide (LINQ to XML)
you can use XML Serialization, in my opinion this is the most comfortable way
of working with c# and xml,
some examples are here:
http://msdn.microsoft.com/en-us/library/58a18dwa(v=vs.110).aspx

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.

Using XPath to read xml

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.

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