Parse an xml string.

Parse an xml string.

Post by Mark Moeyken » Sun, 17 Mar 2002 04:40:15



Hi Everyone,

I have a web service that returns an xml string like this:
<USERS UserID="1" FirstName="Mark" LastName="Moeykens" CompanyName="Mark
Moeykens, Inc."/>

What is the best way to break this string apart to get the values out of it?

Thanks,
Mark Moeykens

 
 
 

Parse an xml string.

Post by MSFT » Sun, 17 Mar 2002 07:28:21


You can wrap it into <?xml ...><root></root>, effectively creating a simple
XML document. Then you can feed the result to the XmlReader and access
attribute/values via DOM.


Quote:> Hi Everyone,

> I have a web service that returns an xml string like this:
> <USERS UserID="1" FirstName="Mark" LastName="Moeykens" CompanyName="Mark
> Moeykens, Inc."/>

> What is the best way to break this string apart to get the values out of
it?

> Thanks,
> Mark Moeykens


 
 
 

Parse an xml string.

Post by Chuck Haeberl » Wed, 20 Mar 2002 08:47:35


Wrapping it in <root/> doesn't make it an xml document -
its valid xml as is...

Use the System.XML.XmlDocument to load the xml, then refer
to the attributes in your node by using the
documentElement property of your XmlDocument, and reading
the values out of the ATtributes collection with
getNamedItem:

XmlDocument myDoc = new XmlDocument();
myDoc.LoadXml(myInString);
string firstName =
myDoc.DocumentElement.Attributes.getNamedItem("FirstName");

etc...

Quote:>-----Original Message-----
>You can wrap it into <?xml ...><root></root>, effectively
creating a simple
>XML document. Then you can feed the result to the

XmlReader and access
>attribute/values via DOM.



>> Hi Everyone,

>> I have a web service that returns an xml string like
this:
>> <USERS UserID="1" FirstName="Mark" LastName="Moeykens"
CompanyName="Mark
>> Moeykens, Inc."/>

>> What is the best way to break this string apart to get
the values out of
>it?

>> Thanks,
>> Mark Moeykens

>.

 
 
 

Parse an xml string.

Post by Mark Moeyken » Wed, 20 Mar 2002 13:25:02


Chuck,

When I looked in the help file I found that the getNamedItem returns a Node,
not a string.

I still apprecite your help.  Yeah, I didn't take the previous guy's advice.
What I ended up doing was reading the string into an xmlTextReader then
using xmlTextReader.GetAttribute("attribute name").

Mark


> Wrapping it in <root/> doesn't make it an xml document -
> its valid xml as is...

> Use the System.XML.XmlDocument to load the xml, then refer
> to the attributes in your node by using the
> documentElement property of your XmlDocument, and reading
> the values out of the ATtributes collection with
> getNamedItem:

> XmlDocument myDoc = new XmlDocument();
> myDoc.LoadXml(myInString);
> string firstName =
> myDoc.DocumentElement.Attributes.getNamedItem("FirstName");

> etc...

> >-----Original Message-----
> >You can wrap it into <?xml ...><root></root>, effectively
> creating a simple
> >XML document. Then you can feed the result to the
> XmlReader and access
> >attribute/values via DOM.



> >> Hi Everyone,

> >> I have a web service that returns an xml string like
> this:
> >> <USERS UserID="1" FirstName="Mark" LastName="Moeykens"
> CompanyName="Mark
> >> Moeykens, Inc."/>

> >> What is the best way to break this string apart to get
> the values out of
> >it?

> >> Thanks,
> >> Mark Moeykens

> >.