suddenly get errors in xml

G

Guest

Hi,
I have a button that, when clicked, saves all the info entered. It worked
fine until suddenly it didn't, and I have no idea why. I get the error
An unhandled exception of type 'System.Xml.XmlException' occurred in
system.xml.dll
Additional information: System error.

I found that this sometimes happens when there's some unrecognized
character, so I even started with a new xml file, but it still won't work.
It yells at me when I try to say XmlNode Catalog = xml.DocumentElement. Any
ideas on why this would suddenly stop working?
Thanks,
Mel

Here's some code:
string cat = @"C:\Catalog4.xml";
XmlDocument xml = new XmlDocument();
xml.Load(cat);

XmlNode Catalog = xml.DocumentElement; (green error arrow here)

XmlElement file = xml.CreateElement("File");
file.InnerText = number.ToString();
Catalog.AppendChild(file);

XmlElement eCust = xml.CreateElement("Cust");
eCust.InnerText = cust;
file.AppendChild(eCust);
xml.Save(cat);
 
M

Martin Honnen

melanieab wrote:

I get the error
An unhandled exception of type 'System.Xml.XmlException' occurred in
system.xml.dll
Additional information: System error.

string cat = @"C:\Catalog4.xml";
XmlDocument xml = new XmlDocument();
xml.Load(cat);

The Load method parses the XML and throws an exception if the markup is
not well formed:
<http://msdn.microsoft.com/library/d.../frlrfSystemXmlXmlDocumentClassLoadTopic2.asp>
So catch the exception e.g.
try {
xml.Load(cat);
}
catch (XmlException e) {
// now check e.Message, e.LineNumber, e.LinePosition here to
// find out why the XML parser can't parse your markup e.g.
}
 
G

Guest

Hi Melanieab,
can you include a copy of what is inside your Catalog4.xml file, that
might help explain more precisely what your problem is.

Mark R Dawson
 
G

Guest

Actually now, after playing around with other sections of the code, it seems
I can't use xml at all anymore. At the beginning, I declare a private
DataTable tCat, then later when I want to load the info into a datagrid, I
say tCat = new DataTable (which worked fine before), but now I get the same
error message as below. I have no idea what's going on.
 
G

Guest

I've tried a few files. This one is Catalog4 (it's just empty except for
what was automatically generated):

<?xml version="1.0" encoding="utf-8" ?>

and I keep getting that same system error when trying to load
string cat = @"C:\Catalog4.xml";
XmlDocument xml = new XmlDocument();
xml.Load(cat);

Next, here's Catalog3:

<?xml version="1.0" encoding="utf-8" ?>
<Catalog>
<File ID="1">
<Cust>mrn</Cust>
<CI>400</CI>
<Date>8/8/2005 2:56:45 PM</Date>
</File>
</Catalog>

Finally, this was my original one (Catalog2) where it just stopped working
after I decided I didn't want to use attributes and changed it from the
format in 3 to this one:

<?xml version="1.0" encoding="utf-8"?>
<Catalog>
<File>1<File>
<Cust>mrn</Cust>
<CI>400</CI>
<Date>8/8/2005 2:56:45 PM</Date>
<File>2<File>
<Cust>mab</Cust>
<CI>444</CI>
<Date>8/8/2005 2:58:23 PM</Date>
</Catalog>

Thanks for looking at it!!!
Mel
 
G

Guest

Hi Melanieab,
I am not sure if you mistyped this for your reply but the XML:
<?xml version="1.0" encoding="utf-8"?>
<Catalog>
<File>1<File>
<Cust>mrn</Cust>
<CI>400</CI>
<Date>8/8/2005 2:56:45 PM</Date>
<File>2<File>
<Cust>mab</Cust>
<CI>444</CI>
<Date>8/8/2005 2:58:23 PM</Date>
</Catalog>

has an invalid <File> tag, the closing bracket is not correct, you have
<File>1<File> when it should be <File>1</File> this would definitely cause
an exception when you try to load the XML and the document tries to parse the
XML.

For your catalog4.xml file, if all it contains is:
<?xml version="1.0" encoding="utf-8" ?>

then an exception will be thrown because there is no document for the XML
document to load, it fails trying to parse the data on the Load method call,
if you have something like:

<?xml version="1.0" encoding="utf-8" ?>
<myRootElement />

then this will load okay. Basically make sure in your code that you always
have a root element in your XML file even if you do not have anything to put
inside it.

Hope that helps
Mark R Dawson
 
G

Guest

To see if the xml is valid, you can open the xml in IE or any browser (that
ships with a parser). The browser will display the part of the file which has
invaild tags. If the XML is valid then the entire XML will be displayed as a
tree.

Thanks,
Manoj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top