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;
Related
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();
I am trying to split a XML file to multiple small xml files in C#.net and am
trying to get the best possible approach to this. Any help on this will be
great... Sample example on what I am trying to do...
Source XML document
<?xml version="1.0" standalone="yes"?>
<DATABASE>
<DOC>
<DOCID>8510188</DOCID>
<ISSUE>2010</ISSUE>
<CAT>Literature and Art</CAT>
<TITLE>Test</TITLE>
<TEXT>Test</TEXT>
</DOC>
<DOC>
<DOCID>1510179</DOCID>
<ISSUE>2012</ISSUE>
<CAT>Miscellaneous</CAT>
<TITLE>Test</TITLE>
<TEXT>Test</TEXT>
</DOC>
</DATABASE>
Should split to two xml documents as below
1)
<?xml version="1.0" standalone="yes"?>
<DATABASE>
<DOC>
<DOCID>8510188</DOCID>
<ISSUE>2010</ISSUE>
<CAT>Literature and Art</CAT>
<TITLE>Test</TITLE>
<TEXT>Test</TEXT>
</DOC>
</DATABASE>
2)
<?xml version="1.0" standalone="yes"?>
<DATABASE>
<DOC>
<DOCID>1510179</DOCID>
<ISSUE>2012</ISSUE>
<CAT>Miscellaneous</CAT>
<TITLE>Test</TITLE>
<TEXT>Test</TEXT>
</DOC>
</DATABASE>
Well, I'd use LINQ to XML:
XDocument doc = XDocument.Load("test.xml");
var newDocs = doc.Descendants("DOC")
.Select(d => new XDocument(new XElement("DATABASE", d)));
foreach (var newDoc in newDocs)
{
newDoc.Save(/* work out filename here */);
}
(I'm assuming you want to save them. Maybe you don't need to. I've tested this just by printing them out to the console instead.)
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.
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;
I am currently using DOM to navigate xml in my C# project. However, some XML that i've come across lately is a bit different.
whereas usually I have:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<entry>
<author>
<name>Me =)</name>
</author>
<content>
<somefield1>
<Subfield>subfield data</subfield>
</somefield>
</content>
</entry>
</feed>
and can navigate using foreach entry as entry, selectsinglenode(/content/somefield1/subfield), innertext to get the data from the subfield for each entry, the new XML looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom">
<atom:entry>
<atom:author>
<name>Me =)</name>
</atom:author>
<atom:content>
<somefield1>
<Subfield>subfield data</subfield>
</somefield>
</atom:content>
</atom:entry>
</atom:feed>
selectsinglenode(/atom:content/somefield1/subfield) is definitely not going to work...any suggestions?
atom: is just the namespace, and possibly you might just ignore it. If it still not works, you may have to use:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
selectsinglenode("atom:content/somefield1/subfield", nsmgr);
Which is documented here