This question already has answers here:
XPath select node with namespace
(6 answers)
How does XPath deal with XML namespaces?
(2 answers)
Closed 5 years ago.
I try to parse an xml like this:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("file.xml");
XmlNode jobNode = xmlDoc.SelectSingleNode("//job-data/schedule/job");
I try to parse this xml:
<?xml version="1.0" encoding="utf-8" ?>
<job-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0">
<schedule>
<job>
<name>ExampleJob</name>
</job>
</schedule>
</job-data>
But my jobNode is alway null.
If i change my xml to this it works:
<?xml version="1.0" encoding="utf-8" ?>
<job-data>
<schedule>
<job>
<name>ExampleJob</name>
</job>
</schedule>
</job-data>
You should use something like this:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", "http://quartznet.sourceforge.net/JobSchedulingData");
XmlNode jobNode = xmlDoc.SelectSingleNode("//ns:job-data/ns:schedule/ns:job");
Related
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>
My Xml as below
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order xmlns="http://example.com/abc">
<class>
<about></about>
</class>
<dataset>
<subjects></subjects>
</dataset>
</order>
I tried to access node by below ways
XmlNode order = myXml.SelectSingleNode("order");
XmlNode subjects= order.SelectSingleNode("/order/dataset/subjects);
XmlNode dataset= myXml.SelectSingleNode("order/dataset");
Even I tried with Xml namespace manager as below
XmlNamespaceManager nsmgr= new XmlNamespaceManager(myXml.NameTable);
nsmgr.AddNamespace("ab","http://example.com/abc");
XmlNode order= myXml.SelectSingleNode("//ab:order", nsmgr);
XmlNode dataset= myXml.SelectSingleNode("//ab:order//ab:dataset",nsmgr);
Where I am doing wrong here? How to access node in this case. Please help me.
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.)
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;
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