Hi All,
I have a xml file like this:
I have a xml file like this:
test1.xml
test2.xml
test3.xml
test4.xml
I am trying to get information/information1['mainlang'] == ENG from xml file.
and also
I would like to get All "lang, group, dataname" from xml file.
I am getting following error "invalid data at the root level. line 1, position the first" for the following code:
XDocument doc = XDocument.Load(path);
XElement test = doc.Root;
var nodeList = doc.SelectNodes("//dataname");
Advance thanks,
Darma
Advance thanks,
Darma
VulpesRank #55 · 29.2K reputationPosted Sep 30, 2014, 5:37 AM EDT
The output is:
Abhishek KumarRank #510 · 4.8K reputationPosted Sep 30, 2014, 1:44 AM EDT
if your xml file would be as below
xml version="1.0" encoding="utf-8" standalone="yes"?>
<informations>
<information>
<information1 name="region">Asiainformation1>
<information1 name="mainlang">ENGinformation1>
information>
<Data>
<dataname lang="Hindi" group="Asia">
test1.xml
dataname>
<dataname lang="Chinese" group="Asia">
test2.xml
dataname>
<dataname lang="English" group="US">
test3.xml
dataname>
<dataname lang="Polish" group="EU">
test4.xml
dataname>
Data>
informations>
i have added a root information.
Please try this code below..
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\My Development\TestWeb\TestWeb\Test.xml");
// Root element
System.Xml.XmlElement root = doc.DocumentElement;
System.Xml.XmlElement information = (System.Xml.XmlElement)root.ChildNodes[0].ChildNodes[0];
string information1 = information.ChildNodes[0].InnerText;
if (information1 == "ENG")
{
//Put your logic here
}
Hope this will help.
Abhsihek
Abhishek KumarRank #510 · 4.8K reputationPosted Sep 30, 2014, 12:58 AM EDT
Your Xml file seems to have issue as it has multiple root level element.
Could you please verify and provide the proper xml.?
Abhishek
darma tejaRank #3,199 · 449 reputationPosted Sep 30, 2014, 1:11 AM EDT
Thanks for the message. Actually It is multiple root level element xml file. I cannot change this xml file, because i get it from customer.
I think we can artificially add a root to this xml file, is it works?
Thanks,
Darma
Abhishek KumarRank #510 · 4.8K reputationPosted Sep 30, 2014, 1:22 AM EDT
Abhishek
darma tejaRank #3,199 · 449 reputationPosted Sep 30, 2014, 1:58 AM EDT
I am getting exception exactly at: doc.Load(@"D:\My Development\TestWeb\TestWeb\Test.xml");
The exception is There are multiple root elements present. Line 8, position the second.
and I would like to get the output
Thanks,
Darma
Abhishek KumarRank #510 · 4.8K reputationPosted Sep 30, 2014, 2:09 AM EDT
D:\My Development\TestWeb\TestWeb\Test.xml
This is my local machine test location for test file , you need to provide the your path where your xml file is residing.
Abhishek
darma tejaRank #3,199 · 449 reputationPosted Sep 30, 2014, 3:29 AM EDT
It means that two root elements present in the xml file.
Thanks,
Darma
darma tejaRank #3,199 · 449 reputationPosted Sep 30, 2014, 4:22 AM EDT
My code is like this:
Thanks,
Darma
Abhishek KumarRank #510 · 4.8K reputationPosted Sep 30, 2014, 4:26 AM EDT
Abhishek
darma tejaRank #3,199 · 449 reputationPosted Sep 30, 2014, 6:41 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts