By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,564 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,564 IT Pros & Developers. It's quick & easy.

Parsing xml text

P: n/a
I have a string which contains some xml text (either in one long line or
split into one line per data element), eg.

"<tag1>some data</tag1><tag2>some more data</tag2>"
or
"<tag1>some data</tag1> (embedded \n
character)
<tag2>some more data</tag2>"

Does the framework provide anything to easily parse this text to get at the
tags and data elements one by one ? I could of course write my own but Im
sure there must be something already there ... I just need to find it.
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Of course ...

Any one one of the various classes under System.Xml will accomplish exactly
this.

XmlTextReader rdr = new XmlTextReader(new StringReader("<tag1>some
data</tag1><tag2>some more data</tag2>"));

rdr.MoveToContent();

while (rdr.Read()){

//Processing here
}

XmlDocument xml = new XmlDocument();
xml.LoadXml("<tag1>some data</tag1><tag2>some more data</tag2>");

and so on ... there are several options for accomplishing this... just read
the documentation

Alex

"JezB" <jb*******@blueyonder.co.yk> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
I have a string which contains some xml text (either in one long line or
split into one line per data element), eg.

"<tag1>some data</tag1><tag2>some more data</tag2>"
or
"<tag1>some data</tag1> (embedded \n
character)
<tag2>some more data</tag2>"

Does the framework provide anything to easily parse this text to get at the tags and data elements one by one ? I could of course write my own but Im
sure there must be something already there ... I just need to find it.

Nov 15 '05 #2

P: n/a
Im having trouble with that because the string isnt a full xml document -
its missing the header information so I get Xml exceptions thrown. Anything
else I can try ?

"Trebek" <tr****@intheformofaquestion.com> wrote in message
news:40***********************@nnrp.fuse.net...
Of course ...

Any one one of the various classes under System.Xml will accomplish exactly this.

XmlTextReader rdr = new XmlTextReader(new StringReader("<tag1>some
data</tag1><tag2>some more data</tag2>"));

rdr.MoveToContent();

while (rdr.Read()){

//Processing here
}

XmlDocument xml = new XmlDocument();
xml.LoadXml("<tag1>some data</tag1><tag2>some more data</tag2>");

and so on ... there are several options for accomplishing this... just read the documentation

Alex

"JezB" <jb*******@blueyonder.co.yk> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
I have a string which contains some xml text (either in one long line or
split into one line per data element), eg.

"<tag1>some data</tag1><tag2>some more data</tag2>"
or
"<tag1>some data</tag1> (embedded \n character)
<tag2>some more data</tag2>"

Does the framework provide anything to easily parse this text to get at

the
tags and data elements one by one ? I could of course write my own but Im sure there must be something already there ... I just need to find it.


Nov 15 '05 #3

P: n/a
Sorry I had 'assumed' you were working with a document. My fault :)

XmlTextReader rdr = new
XmlTextReader("<tag>sometesting</tag>",XmlNodeType.Element,null);

rdr.MoveToContent();

//Do whatever you need to with it

Debug.WriteLine(rdr.ReadOuterXml());
"JezB" <jb*******@blueyonder.co.yk> wrote in message
news:eI**************@tk2msftngp13.phx.gbl...
Im having trouble with that because the string isnt a full xml document -
its missing the header information so I get Xml exceptions thrown. Anything else I can try ?

"Trebek" <tr****@intheformofaquestion.com> wrote in message
news:40***********************@nnrp.fuse.net...
Of course ...

Any one one of the various classes under System.Xml will accomplish

exactly
this.

XmlTextReader rdr = new XmlTextReader(new StringReader("<tag1>some
data</tag1><tag2>some more data</tag2>"));

rdr.MoveToContent();

while (rdr.Read()){

//Processing here
}

XmlDocument xml = new XmlDocument();
xml.LoadXml("<tag1>some data</tag1><tag2>some more data</tag2>");

and so on ... there are several options for accomplishing this... just

read
the documentation

Alex

"JezB" <jb*******@blueyonder.co.yk> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
I have a string which contains some xml text (either in one long line or split into one line per data element), eg.

"<tag1>some data</tag1><tag2>some more data</tag2>"
or
"<tag1>some data</tag1> (embedded \n character)
<tag2>some more data</tag2>"

Does the framework provide anything to easily parse this text to get
at
the
tags and data elements one by one ? I could of course write my own but

Im sure there must be something already there ... I just need to find it.



Nov 15 '05 #4

P: n/a
I actually did it the XmlDocument way but just padded my string with <HDR>
.... </HDR> and parsed the document 2 levels deep, which worked fine. Thanks.

"Trebek" <tr****@intheformofaquestion.com> wrote in message
news:40***********************@nnrp.fuse.net...
Sorry I had 'assumed' you were working with a document. My fault :)

XmlTextReader rdr = new
XmlTextReader("<tag>sometesting</tag>",XmlNodeType.Element,null);

rdr.MoveToContent();

//Do whatever you need to with it

Debug.WriteLine(rdr.ReadOuterXml());
"JezB" <jb*******@blueyonder.co.yk> wrote in message
news:eI**************@tk2msftngp13.phx.gbl...
Im having trouble with that because the string isnt a full xml document -
its missing the header information so I get Xml exceptions thrown. Anything
else I can try ?

"Trebek" <tr****@intheformofaquestion.com> wrote in message
news:40***********************@nnrp.fuse.net...
Of course ...

Any one one of the various classes under System.Xml will accomplish

exactly
this.

XmlTextReader rdr = new XmlTextReader(new StringReader("<tag1>some
data</tag1><tag2>some more data</tag2>"));

rdr.MoveToContent();

while (rdr.Read()){

//Processing here
}

XmlDocument xml = new XmlDocument();
xml.LoadXml("<tag1>some data</tag1><tag2>some more data</tag2>");

and so on ... there are several options for accomplishing this... just

read
the documentation

Alex

"JezB" <jb*******@blueyonder.co.yk> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
> I have a string which contains some xml text (either in one long line or > split into one line per data element), eg.
>
> "<tag1>some data</tag1><tag2>some more data</tag2>"
> or
> "<tag1>some data</tag1>
(embedded
\n
> character)
> <tag2>some more data</tag2>"
>
> Does the framework provide anything to easily parse this text to get at the
> tags and data elements one by one ? I could of course write my own

but Im
> sure there must be something already there ... I just need to find

it. >
>



Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.