Need to add xml element to last parent using linq c# - c#

I'm really new to Linq and C# and I'm stuck on what is probably an obvious problem.
I have an existing XML file
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<books>
<book>
<title>This is Title 1</title>
<author>John Doe</author>
<categories>
<category>How to</category>
<category>Technical</category>
</book>
<book>
<title>This is Title 2</title>
<author>Jane Brown</author>
<categories>
<category>Fantasy</category>
</categories>
</book>
</books>
I want to add a 2nd category to the second book in this file.
I've gotten this far:
var thiscat = doc.Root
.Element("book")
.Element("categories");
thiscat.Add(new XElement("category", "novel"));
But this adds a 3rd category to the first book. I need to learn how to point 'thiscat' at the last categories element rather than the first one. I've been sniffing around LastNode but haven't managed to get the syntax right.
This is my first question here. Please let me know if I'm not being clear or if I'm doing anything wrong.

Pete,
Here is an example that will search for the book by title This is Title 2 and add another category.
var elem = doc.Root.Elements("book").FirstOrDefault(x => x.Element("title").Value.Equals("This is Title 2"));
if (elem != null)
{
var category = elem.Element("categories");
category.Add(new XElement("category", "novel"));
}
Edit: More explanatoin.
First of we search the documents book elements for the matching title of This is Title 2 (effectively your second entry). By executing the FirstOrDefault extension method we either the get the first matching element (as XElement) or null.
Because we 'could' get a null value we must check if the value is null if not we move into the next step of locating the categories element. This can be done simply calling the elem.Element() method as we only expect one element.
Finally we add a new XElement to the category element.
Hope this helps.
Cheers.

To answer your question quite literally, you could modify the statement as follows:
var thiscat = doc.Root
.Elements("book")
.Skip(1)
.First()
.Element("categories");
The "Element" function returns the first element of that type found. In this case, we used "Elements" instead to return an IEnumerable containing all of the elements named "book", and then we used the LINQ "skip" function to skip the first (returning another IEnumerable of all the remaining elements), and then we took just the first element in the IEnumerable (back to a single XElement).
Another way you could have gotten to the answer is as follows:
var thiscat = doc.Root
.Element("book")
.ElementsAfterSelf()
.First()
.Element("categories");
ElementsAfterSelf returns an IEnumerable of all the sibling elements after the calling object.
LINQ is a really critical part of programming in C# and it's good to see you're trying to learn it from the beginning. Although your methodology here in adding a specific element to a specific place programmatically is questionable (obviously it is a contrived example), in playing around like this you will probably learn a bit about LINQ and that is always good.

First you should get your second book element.According to your code:
var thiscat = doc.Root
.Element("book")
.Element("categories");
This statement returns just one categories element which belongs to your first book.Because you are using Element instead of Elements. Let's go step by step.
A proper way to get second element is using Descendants like this:
var secondBook = doc.Descendants("book")[1];
Descendants returning a collection of your books.And we are getting second element with indexer.Now we need to select your categories element under the book element.
var categories = secondBook.Element("categories");
Now we have our categories element and we can add our new category and save Xml Document:
categories.Add(new XElement("category", "novel"));
doc.Save(path);
And that's all.If you understand that logic you can modify your html file however you like.Besides you can make all of these in one line:
doc.Descendants("book")[1]
.Element("categories")
.Add(new XElement("category", "novel"));

This should work( slightly lengthy solution as it helps understand the fundamentals better):
XmlElement rootNode = xd.DocumentElement; //gives <books> the root node
XmlNodeList cnodes= rootNode.ChildNodes; //gets the childnodes of <books>
XmlNode secondBook= cnodes.Item(1); //second child of <books> i.e., the <book> you want
XmlNodeList bnodes= secondBook.ChildNodes; //gets the childnodes of that <book>
XmlNode categories= bnodes.Item(2); //gets the third child i.e.,<categories>
//making the new <category> node
string xmlContent = "<category>novel</category>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlContent);
XmlNode newNode = doc.DocumentElement;
//making the new node completes
categories.AppendChild(newNode); //append the new node to <categories> as a child

Related

XDocument - Iterating Over XML Elements

Given an example XML file as such:
<libraries>
<library name="some library">
<book name="my book"/>
<book name="your book"/>
</library>
<library name="another library">
<book name="his book"/>
<book name="her book"/>
</library>
</libraries>
How would one iterate through each library and get only its children? E.g. if I was in the first library element and I went to retrieve all its descendants/children, it would only return with the two books inside it.
I've tried iterating and using XElement.Elements("book"), XElement.Elements(), XElement.Descendants(), etc. but all return every element that is a book (so it would pull the elements from the second library, too). Mostly I think I'm just struggling with understanding how XDocument keeps track of its elements and what's considered a descendant/child.
If possible, if one could explain as to how this would be done with XDocument for an element at any level it'd be appreciated (e.g. if each book had child elements, and if those elements had child elements, etc).
You can iterate over your XML by going through all the descendents of libraries in the following way.
XDocument doc=XDocument.Load(XmlPath);
foreach (var item in doc.Descendants("library"))
IEnumerable<XNode> nodes = item.DescendantNodes();//Here you got book nodes within a library
Sheer,
The problem is you are pulling all elements with "book".
If you want to get only items dependant on the parent element, you will have to supply a proper condition.
var v = from n in doc.Descendants("library")
where n.Attribute("name").Value == "some library"
select n.DescendantNodes();
Now, this will give you element who's name is "some library".

Accessing nested elements while iterating an XML LINQ query?

With this XML data:
<item>
<placemarks>
<placemark>
<uid>{EA5FA2B2-78CB-4FAA-9F17-EBB361410499}</uid>
</placemark>
</placemarks>
<links>
<link>
<title>Book Online</title>
<link>https://blah</link>
</link>
<link>
<title>Call to Book</title>
<link>tel:1-866-555-5555</link>
</link>
</links>
</item>
I'm trying to create Hotel objects w/ various attributes from the XML. Everything works fine until I hit nested XML tags and then I got stuck:
var items = root.Descendants ("item");
var hotels = from it in items
select new Hotel () {
Uid = it.Element ("uid").Value,
Name = it.Element ("name").Value,
Description = it.Element ("description").Value,
ImageUrl = it.Element ("image_url").Value,
RoomType = it.Element("custom_1").Value,
PlacemarkId = it.Element("placemarks").Element("placemark").Element("uid").Value,
BookUrl = (from links in it.Descendents("links") where links.Element("link").Element("title) = "Book Online").Value
};
How do I get PlacemarkId to work? I keep getting null because the methods after the first Element("placemarks") evidently fails :-(
And obviously, setting the BookUrl property won't compile, but that's what I'd like to do. It's really ugly because of the weird XML schema w/ nested link tags :-(
Sorry for the noob question. I tried googling for every combo of "nested xml linq select" I could think of w/ no luck :-P
Would help even more if someone can let me know what I'm trying to do is called in LINQ. I would think it's possible...
Thanks in advance :-)
You can use XPathSelectElement() extension method to avoid null reference exception in case some <item> don't have placemark child (without having to manually check for nulls from C# code) :
var hotels = from it in items
select new Hotel()
{
......
......
PlacemarkId = (string)it.XPathSelectElement("placemarks/placemark/uid"),
BookUrl = (string)it.XPathSelectElement("links/link[title='Book Online']/link"),
};
Getting BookUrl value can also be done using XPath as demonstrated above. Or if you're sure the XML structure is consistent for this part (no element is ever missing), you can use LINQ without null checking like so :
BookUrl = (from link in it.Elements("links").Elements("link")
where (string)link.Element("title") == "Book Online"
select link.Element("link")).First().Value
For reference :
W3C : XML Path Language (XPath) Version 1.0 specification (formal & complete spec of xpath)
w3schools : XPath syntax (relaxed intro to basic xpath syntax)

C# get all children of XElement regardless of their value

I have the following XML structure:
<init_deinit>
<step name="init">
<call>...</call>
<check>...</check>
<call>...</call>
<wait>...</wait>
....
</step>
<step name="deinit">
....
</step>
</init_deinit>
There is a lot of examples of how to retrieve all descendants of a single type. I.E.:
XDocument xdoc = XDocument.Load("file.xml")
var all_call_tags = xdoc.Descendants("init_deinit").Elements("step").ElementAt(0).Elements("call");
But I need to retrieve ALL the children of the 'step' element and I need to retrieve them in the exact order thay are written in the XML. So what I need is something like IEnumerable iterator that contains XElements call, check, call and wait in this order. I tried but failed so far :)
Thank you for your advice!
This will give you all Descendants of step elements:
xdoc.Descendants("step").SelectMany(x => x.Descendants());
If you want Descendants of first step element use
xdoc.Descendants("step").First().Descendants();
Please try this :
XDocument xdoc = XDocument.Load("file.xml");
//Here you will get all the descendants of the first step
xdoc.Descendants("step").First().Descendants();
//To get all Descendants of step elements:
var x = xdoc.Descendants("step").Descendants();

Unique nodes and c#

Is there a good way in c# to look through an XML node list using DOM and get a node list of only the unique nodes and also a list of each nodes unique possible attributes.
The XMl file in question has nodes of the same name but with different attributes, i want a list of all the possible ones. Also the list of nodes i would like to be only of the unique nodes, rather than having repeats (so node lists i generate at the moment might have contact twice, three time ect within it). And it needs to work for any XML document. Any ideas?
Here is an example:
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
</book>
<book id="bk162">
<genre>fiction</genre>
<popularity>High</popularity>
<price>20.00</price>
<publish_date>2002-03-12</publish_date>
</book>
<cd id="bk162">
<genre>jaz</genre>
<popularity>High</popularity>
<price>10.00</price>
</cd>
and get some sort of output like:
there are 2 of the type book
there are 1 of the type cd
there are 3 of the type genre
book may have the attributes author, title, genre, price, popularity, publish_date
but in a way that works for any xml file.
In the case of genre it doesnt need to be celver in any way, just know there are 3 genre nodes in the document.
Would this do it?
XDocument xDoc = XDocument.Load("XMLFile1.xml");
List<XElement> distinctDocs = xDoc.Descendants().GroupBy(x => x.Name).Where(x => x.Count() == 1).Select(g => g.Single()).ToList();

Select child nodes, but ignore non-elements with XPath?

Given the following XML document for example:
<?xml version="1.0"?>
<UrdaObject>
<Date>
<Year>2011</Year>
<Month>5</Month>
<Day>18</Day>
<Hours>8</Hours>
<Minutes>47</Minutes>
<Seconds>36</Seconds>
</Date>
<random_value>24</random_value>
</UrdaObject>
And the understanding the child::node() - Selects all child nodes of the current node how would I create an XPath (starting from the root) that would select all child nodes EXCEPT text, comments, and other things that are NOT elements. For example, when using this code to create a tree view in WPF:
// x is some XmlDocument, xmlTree is my WPF TreeView
XmlDataProvider provider = new XmlDataProvider();
provider.Document = x;
Binding binding = new Binding();
binding.Source = provider;
binding.XPath = "child::node()";
xmlTree.SetBinding(TreeView.ItemsSourceProperty, binding);
How would I go about creating my XPath statement so I build a treeview with nodes going all the way down and stopping before the raw text? For example it would generate a view of:
UrdaObject
Date
Year
...
Instead of...
UrdaObject
Date
Year
2011 (Don't want this!)
...
The sample XML files is just for me to explain my situation. The expression should be able to navigate any valid XML file and pull the elements, but not the individual text.
How did we fix this? I had switched all references of child::node() to child::*. However, I had NOT corrected one line in my XAML, which was pulling child::node(). Correcting this line made the application behave correctly... and made me feel silly.
child::node() finds all child nodes. child::* finds all element nodes.
it's as simple as *.
(that gets immediate children, however; if you want all descendant elements, it would be descendant::*)
child::* will exclude text nodes and leave only element nodes
child::text() will include only text nodes
child::node() will include both element and text nodes
http://www.w3.org/TR/xpath/#location-paths
Not sure if this is what you want but could it be done this way?
var doc =XDocument.Parse(#"
<UrdaObject>
<Date>
<Year>2011</Year>
<Month>5</Month>
<Day>18</Day>
<Hours>8</Hours>
<Minutes>47</Minutes>
<Seconds>36</Seconds>
</Date>
<random_value>24</random_value>
</UrdaObject>
");
var query = from s in doc.Descendants()
select s.Name;
foreach (var name in query)
{
Console.WriteLine(name);
}

Categories