How to parse XML?

  • Thread starter Thread starter nomad
  • Start date Start date
N

nomad

Hi,

I am writing a maintenance site for my department. One of the pages
displays XML from the database in a GridView. I would like to be able
to edit the cell, which I can currently do, but parse the contents of
the cell when updating to make sure what is saved into the databse is
proper XML.

Any suggestions on how I could do this would be greatly appreciated.


Thanks
 
Hi,

I am writing a maintenance site for my department.  One of the pages
displays XML from the database in a GridView.  I would like to be able
to edit the cell, which I can currently do, but parse the contents of
the cell when updating to make sure what is saved into the databse is
proper XML.

Any suggestions on how I could do this would be greatly appreciated.

Thanks

Use the XmlDocument class.

XmlDocument doc = new XmlDocument();
string xmlData = .... from the database

doc.Load(new StringReader(xmlData));

XmlNodeList nodes = xml.SelectNodes("//topic");

string prefix;
foreach (XmlNode node in nodes)
{
....
}

doc.Save(xmlData);

...save xmlData to the database

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
 
Use the XmlDocument class.

XmlDocument doc = new XmlDocument();
string xmlData = .... from the database

doc.Load(new StringReader(xmlData));

XmlNodeList nodes = xml.SelectNodes("//topic");

string prefix;
foreach (XmlNode node in nodes)
{
....

}

doc.Save(xmlData);

...save xmlData to the database

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx

HI,

Thanks very much for your reply. I'm gathering by your code that by
doing doc.Load(new StringReader(xmlData)); it is basically parsing the
XML, and if this fails then I trap that error. If it successds then I
save the xmlData back into the database?

THanks for your time.
 
HI,

Thanks very much for your reply.  I'm gathering by your code that by
doing doc.Load(new StringReader(xmlData)); it is basically parsing the
XML, and if this fails then I trap that error.  If it successds then I
save the xmlData back into the database?

THanks for your time.- Hide quoted text -

- Show quoted text -

I think it would save the XmlDocument... What I forgot to mention is
about special/escape characters in XML (is it what you trying to avoid
of?). Look at the following article

How to locate and replace special characters in an XML file with
Visual C# .NET
http://support.microsoft.com/kb/316063

Hope this helps
 
I think it would save the XmlDocument... What I forgot to mention is
about special/escape characters in XML (is it what you trying to avoid
of?). Look at the following article

How to locate and replace special characters in an XML file with
Visual C# .NEThttp://support.microsoft.com/kb/316063

Hope this helps

Hi,

Thanks again for the prompt reply. What I'm basically after is that
if someone makes a change to the XML in the textbox, I want to verify
that it is well formed XML before saving it into the database. If
it's not well formed then I can display a message box, which I have
already written, explaining to the user that it's not valid XML. They
can then make the relevant change before saving it.

Hope that makes sense.

Really appreciate your help.
 
Hi,

Thanks again for the prompt reply.  What I'm basically after is that
if someone makes a change to the XML in the textbox, I want to verify
that it is well formed XML before saving it into the database.  If
it's not well formed then I can display a message box, which I have
already written, explaining to the user that it's not valid XML.  They
can then make the relevant change before saving it.

Hope that makes sense.

Really appreciate your help.- Hide quoted text -

- Show quoted text -


If you would edit it in the textbox, then you can simply use the
XmlTextReader:
http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.aspx

XmlReader r = new XmlTextReader(new StringReader(TextBox1.Text));

try {
while (r.Read())
{
//XML is well-formed
} catch (Exception e) {
//error
}

And if XML is not syntactically correct, the XML parser will raise an
error.
 
Back
Top