How to parse XML?

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
 
G

Guest

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
 
N

nomad

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.
 
G

Guest

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
 
N

nomad

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.
 
G

Guest

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.
 

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

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top