I am running a C# application on the raspberry pi in which i read an xml file once at startup and then each time the file changes using this code:
using System.Xml;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(settingsFileName);
The first time (at startup) it always works, however when the file changes i always get this exception:
Unhandled exception. System.Xml.XmlException: Root element is missing.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(String filename)
The config file looks like:
<config>
<data1 seconds = "5000"></data1>
<data2 check = "16:00"></data2>
<data3 max = "50"></data3>
</config>
and off course i only change data and not the root element "config".
What could be causing this?
EDIT:
Putting <?xml version="1.0"?> at the top of the file improves a little, now it only gives the error the second time i change the file.
There is no code that changes the file, there is however code that checks the last saved time of the file and if that is different compared to the initial one, then i read the xml file again. The actual change of the xml file I do myself with the default editor of winscp (as i am using SSH to access the pi).
dotnet --version 3.1.425
Related
As the title says, I have a problem loading an XML file with XMLDocument.Load() where that will error in Linux with "Root element is missing" but in Windows it will load perfectly fine.
I have a root element in my XML as needed.
Below is the XML file and code snippet which loads the XML:
XML (Message.xml):
<sms>
<tag1></tag1>
<tag2></tag2>
.
.
.
</sms>
Code snippet:
XMLDocument msg = new XMLDocument();
msg.Load(path + #"/Message.xml") //I have tried to add an "#" and to remove it - no change
//Exception is thrown
I am using Path.Combine() to create the path string needed.
I have a random exception when trying to retrieve and load an XML file through SOAP protocol like this
XmlDocument xml = new XmlDocument();
using (Stream stream = webRequest.GetResponse().GetResponseStream())
{
xml.Load(stream); //<-- Exception here
}
I did test of files with a size of 267MB. It is a system integration where a system generate a XML with all the employees' data including picture of their profiles. Everything worked well. But recently I am experiencing some exceptions with this detail:
at System.Text.StringBuilder.ToString()
at System.Xml.XmlTextReaderImpl.ParseText()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(Stream inStream)
I wonder if there is a size limit on each node. The file only contained data with a size of 15MB, but once the file included in a node a picture in base64 the size increased to 260M, but still worked well on tests and production.
It is lately that I have this problem.
Before I get into the issue, I'm aware there is another question that sounds exactly the same as mine. However, I've tried that solution (using Notepad++ to encode the xml file as UTF-8 (without BOM) ) and it doesn't work.
XmlDocument namesDoc = new XmlDocument();
XmlDocument factionsDoc = new XmlDocument();
namesDoc.LoadXml(Application.persistentDataPath + "/names.xml");
factionsDoc.LoadXml(Application.persistentDataPath + "/factions.xml");
Above is the code I have problems with. I'm not sure what the problem is.
<?xml version="1.0" encoding="UTF-8"?>
<factions>
<major id="0">
...
Above is a section of the XML file (the start of it - names.xml is also the same except it has no 'id' attribute). The file(s) are both encoded in UTF-8 - in the latest notepad++ version, there is no option of "encode in UTF-8 without BOM" afaik UTF-8 is the same as UTF-8 without BOM.
Does anyone have any idea what the cause may be? Or am I doing something wrong/forgetting something? :/
You are receiving an error because the .LoadXml() method expects a string argument that contains the XML data, not the location of an XML file. If you want to load an XML file then you need to use the .Load() method, not the .LoadXml() method.
I have a simple code which modifies an existing XML file as follows:
roomsfile = new File(Path.Combine(rootDir, "rooms.dat"));
XmlDocument roomdata = new XmlDocument();
roomdata.Load(roomsfile.AbsolutePath);
XmlElement root = roomdata.DocumentElement;
XmlNode roomNode = roomdata.CreateElement("room");
XmlNode noNode = roomdata.CreateElement("no");
XmlNode nameNode = roomdata.CreateElement("name");
//roomNo and roomName are text fields
noNode.InnerText = roomNo.Text;
nameNode.InnerText = roomName.Text;
roomNode.AppendChild(noNode);
roomNode.AppendChild(nameNode);
root.AppendChild(roomNode);
roomdata.Save(roomsdat.AbsolutePath);
My original XML file is:
<?xml version="1.0" encoding="utf-8"?>
<rooms>
<room>
<no>101</no>
<name>Reception</name>
</room>
<room>
<no>102</no>
<name>Manager Room</name>
</room>
</rooms>
When I run the code it turns the file into:
<?xml version="1.0" encoding="utf-8"?>
<rooms>
<room>
<no>101</no>
<name>Reception</name>
</room>
<room>
<no>102</no>
<name>Manager Room</name>
</room>
<room>
<
So it's clearly incomplete. And then it broke my app when I try to use the file. What would cause this kind of behaviour?
For the record, this application is being written on Xamarin (MonoDroid) in Visual Studio 2013.
Ok, so I found out that it's just the way my XML file appears while the app's still using it. Even if I copy the file to my PC and view it with Notepad++, it still seems broken. But the app uses it just as expected, without any problems (it was apparently another issue that broke it before). A weird behaviour, if you ask me. But I won't dig it any further.
Anyway, in the comments, dbc pointed out a simple but cool tip about overwriting files, which I believe is crucial in preventing potential corrupt file issues in any app. I'll use this as a rule of thumb for the rest of my coding life:
Writing to a new file, then deleting the old and renaming the new, is actually good practice. That way, if anything happens while writing the file (e.g. the disk fills up, or the user kills your process) then the original file is untouched.
I want to load an XML file located on my server so I can get the value of the XML Element called "CheckInterval" and store it on a string called "NewIntervalSet".
I am loading the following XML file called "ConfigFile.xml".
<?xml version="1.0" encoding="utf-8"?>
<Cart>
<CartConfiguration>
<CheckInterval>0.25</CheckInterval>
</CartConfiguration>
</Cart>
The way that I am loading it is the following:
XElement xelement;
xelement = XElement.Load(Path.Combine("\\\\server\\public\\Engineering","ConfigFile.xml"));
The way that I'm storing the XML element "CheckInterval" into the string "NewIntervalSet" is the following:
string NewIntervalSet;
NewIntervalSet=xelement.Descendants("CartConfiguration")
.Select(x => x.Element("CheckInterval").Value).FirstOrDefault();
When I place a breakpoint where the file is being loaded I can see that the file is loading correctly, so I know the path is right, but when it tries to select the XML element it skips this line of code and it returns a null value, therefore a null string on the "NewIntervalSet" variable. I have no idea why is doing this, when I use the same code but the path is on the local machine it works correct.
Your program may be running into a permissions issue. According to MSDN, XElement creates by calling XmlReader.Create, which in turn has the following to say
A default XmlUrlResolver with no credentials is used to access any
external resources such as a document type definition (DTD), entities,
schemas, and so on. If the external resource is located on a network
resource that requires authentication, specify an XmlResolver with the
necessary credentials using the XmlReaderSettings.XmlResolver
property.
Since your XML document is located on a network path, it's using default/null credentials, causing it to get no read permissions and an empty document. Try opening the file as a stream so you can make a run where it reads out text, and then pipe that stream into a new XElement using this overload. Alternatively, instantiate the XmlResolver yourself so you can set the credentials.
I fix this problem by loading the XML file as an XDocument and not as an XElement. The new way that I'm loading the XML file is the following:
XDocument xDocument;
xDocument= XDocument.Load(Path.Combine("\\\\server\\public\\Engineering","ConfigFile.xml"));