473,322 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to parse XML?

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
Aug 5 '08 #1
6 1519
On Aug 5, 9:00*pm, nomad <d.bedg...@o2.co.ukwrote:
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/libr...ldocument.aspx
Aug 5 '08 #2
On Aug 5, 8:50*pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
On Aug 5, 9:00*pm, nomad <d.bedg...@o2.co.ukwrote:
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/libr...ldocument.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.
Aug 5 '08 #3
On Aug 5, 9:59*pm, nomad <d.bedg...@o2.co.ukwrote:
On Aug 5, 8:50*pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:


On Aug 5, 9:00*pm, nomad <d.bedg...@o2.co.ukwrote:
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/libr...ldocument.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.- 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
Aug 5 '08 #4
On Aug 5, 9:18*pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
On Aug 5, 9:59*pm, nomad <d.bedg...@o2.co.ukwrote:
On Aug 5, 8:50*pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
On Aug 5, 9:00*pm, nomad <d.bedg...@o2.co.ukwrote:
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 beable
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/libr...ldocument.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.- 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# .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.
Aug 5 '08 #5
On Aug 5, 10:30*pm, nomad <d.bedg...@o2.co.ukwrote:
On Aug 5, 9:18*pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:


On Aug 5, 9:59*pm, nomad <d.bedg...@o2.co.ukwrote:
On Aug 5, 8:50*pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
On Aug 5, 9:00*pm, nomad <d.bedg...@o2.co.ukwrote:
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/libr...ldocument.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.- 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# .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.- 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/libr...extreader.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.
Aug 5 '08 #6
On Aug 5, 10:36*pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
On Aug 5, 10:30*pm, nomad <d.bedg...@o2.co.ukwrote:
On Aug 5, 9:18*pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
On Aug 5, 9:59*pm, nomad <d.bedg...@o2.co.ukwrote:
On Aug 5, 8:50*pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
On Aug 5, 9:00*pm, nomad <d.bedg...@o2.co.ukwrote:
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/libr...ldocument.aspx
HI,
Thanks very much for your reply. *I'm gathering by your code thatby
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# .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.- 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/libr...extreader.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.
Thank you very much; that's exactly what I'm after.
Aug 6 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to...
24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
3
by: Jon Davis | last post by:
The date string: "Thu, 17 Jul 2003 12:35:18 PST" The problem: // this fails on PST DateTime myDate = DateTime.Parse("Thu, 17 Jul 2003 12:35:18 PST"); Help? Jon
3
by: Mark | last post by:
How do you parse a currency string to a decimal? I'd like to avoid having to parse the number out, removing the $ manually. That sounds like a hack. There are times that this string will be...
14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
3
by: Bob Rundle | last post by:
I would like to get something like this to work... Type t = FindMyType(); // might be int, float, double, etc string s = "1233"; object v = t.Parse(s); This doesn't work of couse, Parse is...
3
by: Slonocode | last post by:
I have some textboxes bound to an access db. I wanted to format the textboxes that displayed currency and date info so I did the following: Dim WithEvents oBidAmt As Binding oBidAmt = New...
5
by: js | last post by:
I have a textbox contains text in the format of "yyyy/MM/dd hh:mm:ss". I need to parse the text using System.DateTime.Parse() function with custom format. I got an error using the following code. ...
2
by: Samuel R. Neff | last post by:
I'm using a quasi open-source project and am running into an exception in double.Parse which is effectively this: double.Parse(double.MinValue.ToString()) System.OverflowException: Value was...
3
by: Peter Duniho | last post by:
I'm sure there's a good explanation for this, but I can't figure it out. I tried using DateTime.Parse() with a custom DateTimeFormatInfo instance, in which I'd replaced the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.