Answered by:
Root element is missing exception

Question
-
Hi Experts,
I have a XML file with below mentioned format.
<?xml version="1.0" standalone="yes"?> <AppConfig> <CurrentState Status=""/> </AppConfig>
I am reading this XML file using below code.
public void SetValue(bool vblnStatus) { try { string strUIPath = @"C:\appconfig.xml"; if (!string.IsNullOrEmpty(strACPath) && File.Exists(strACPath)) { DataSet ds = new DataSet(); ds.ReadXml(strACPath); if (!ReferenceEquals(null, ds) && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { DataRow dr = ds.Tables[0].Rows[0]; //Get the Edited row ds.Tables[0].Rows[0]["Status"] = vblnStatus.ToString(); ds.WriteXml(strACPath); } ds.Dispose(); ds = null; } } catch (Exception ex) { } }
This method of reading XML file is working correctly in my code and when I am debugging, it is working properly.
BUT
When I deployed this code and installed in users machine (may be windows 7 or windows xp or windows vista) I am receiving this error.
System.Xml.XmlException: Root element is missing. at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res) at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlReader.MoveToContent() at System.Data.DataSet.ReadXml(XmlReader reader, Boolean denyResolving) at System.Data.DataSet.ReadXml(String fileName)
I have searched my entire day to resolve this issue in google but found no solutions.
Please please please help me to solve this issue.
Note : I am accessing the XML file in every second.
Regards,
P. Paul
- Moved by Kee Poppy Friday, May 11, 2012 3:31 AM (From:Windows Presentation Foundation (WPF))
Tuesday, May 8, 2012 1:07 PM
Answers
-
As a side note, why are you going through such hooplas to read and write the Xml?
Try reading it like so:
XElement xml = XElement.Load("appconfig.xml");
String currentStatus = (String)xml.Element("CurrentState").Attribute("Status");Or simply:
String currentStatus = (String)XElement.Load("appconfig.xml")
.Element("CurrentState").Attribute("Status");
John Grove, Senior Software Engineer http://www.digitizedschematic.com/
Thursday, May 17, 2012 2:01 PM -
If the file path is correct, the only other thing that could cause this issue is if the XML file does not look like what you expect. This exception is thrown if you have multiple root-level elements (i.e. in your example, if you have two <AppConfig> elements).
Double-check the XML file you're reading in production, and make sure it only exactly one root-level element and everything else falls under that element. You may also take the XML file and pass it through a validator to make sure it doesn't have any syntax problems anywhere in it. Every time I have ever seen this error, it has been due to a bad XML file.
Check out My Blog. Now updated to actually work!
- Marked as answer by Bob Shen Wednesday, May 23, 2012 9:30 AM
Thursday, May 17, 2012 2:03 PM
All replies
-
Hi P.Paul,
It does not sound like a WPF issue. We have Visual C# forum would fit this question better.
I will move this thread there for getting more efficient responses.
Thanks,
Kee Poppy [MSFT]
MSDN Community Support | Feedback to us
Friday, May 11, 2012 3:31 AM -
Hi,
I can see the "strUIPath". But what is "strACPath"?
Always mark the answers if the post answers your question. Prabhu R
Friday, May 11, 2012 3:53 AM -
Hi P.Paul,
I got your issue, this issue can be raised also when it doesn't detect the file path in the deployed system. I suggest you please find that the file path is given correctly and more over it has read/write permissions.
Generally, Dataset.Readxml() method expects an valid path if doesn't found it doesn't raises File Not Found Exception instead internally it sends null object to the XmlDocument object.. so this issue will be raised.
Hope this will Help you :)
Sai Kumar K http://www.santoshtechnologies.com http://saimaterial.wordpress.com
- Edited by Sai Kumar Koona Friday, May 11, 2012 7:10 AM
- Proposed as answer by Suresh Narayanaswamy Wednesday, May 23, 2012 5:22 PM
Friday, May 11, 2012 7:09 AM -
Hi Prabhu,
strUIPath and strACPath is same. I have mistyped it.
Regards,
P.Paul
Thursday, May 17, 2012 1:34 PM -
Hi Sai,
File path in the deployed system is correct and it has read/write permissions.
Regars,
P.Paul
Thursday, May 17, 2012 1:35 PM -
As a side note, why are you going through such hooplas to read and write the Xml?
Try reading it like so:
XElement xml = XElement.Load("appconfig.xml");
String currentStatus = (String)xml.Element("CurrentState").Attribute("Status");Or simply:
String currentStatus = (String)XElement.Load("appconfig.xml")
.Element("CurrentState").Attribute("Status");
John Grove, Senior Software Engineer http://www.digitizedschematic.com/
Thursday, May 17, 2012 2:01 PM -
If the file path is correct, the only other thing that could cause this issue is if the XML file does not look like what you expect. This exception is thrown if you have multiple root-level elements (i.e. in your example, if you have two <AppConfig> elements).
Double-check the XML file you're reading in production, and make sure it only exactly one root-level element and everything else falls under that element. You may also take the XML file and pass it through a validator to make sure it doesn't have any syntax problems anywhere in it. Every time I have ever seen this error, it has been due to a bad XML file.
Check out My Blog. Now updated to actually work!
- Marked as answer by Bob Shen Wednesday, May 23, 2012 9:30 AM
Thursday, May 17, 2012 2:03 PM