Parsing XML

M

mick

Been having a go at learning a bit about web services and WCF. Got that bit
working only
to get stuck at parsing the Response. I could simply parse the string myself but
I tried to
have a go at using LINQ to XML. Spent hours messing about with this last night
and still
couldn't work out how to reach particular elements and get their value. Still as
clueless
as when I started:)

So given that:

string res; //is the Response I got

var document = XElement.Parse(res)

or

var document = XDocument.Parse(res)

I'm not sure which one I'm supposed to use; any hints as to which and (in *very*
simple
terms), why?

Given the Response below, how would I get, for example, the value of the temp_C
Element?

If I wanted to get a collection of all of the Elements within the <data> Element
(that is, not
just its own child Element but every one between <data> and </data>), how would
I do that?

What are the major benefits of using Linq as opposed to just parsing the
response string
myself? Are their any other ways?

I use Linq to objects and XAML a lot but never tried to parse xml/xaml, and it
shows:)



<data>
<request>
<type>City</type>
<query>London, United Kingdom</query>
</request>
<current_condition>
<observation_time>11:11 AM</observation_time>
<temp_C>10</temp_C>
<temp_F>50</temp_F>
<weatherCode>113</weatherCode>
<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]></weatherIconUrl>
<weatherDesc><![CDATA[Sunny]]></weatherDesc>
<windspeedMiles>14</windspeedMiles>
<windspeedKmph>22</windspeedKmph>
<winddirDegree>250</winddirDegree>
<winddir16Point>WSW</winddir16Point>
<precipMM>0.0</precipMM>
<humidity>76</humidity>
<visibility>10</visibility>
<pressure>1026</pressure>
<cloudcover>25</cloudcover>
</current_condition>
<weather>
<date>2013-11-29</date>
<tempMaxC>9</tempMaxC>
<tempMaxF>48</tempMaxF>
<tempMinC>3</tempMinC>
<tempMinF>37</tempMinF>
<windspeedMiles>16</windspeedMiles>
<windspeedKmph>25</windspeedKmph>
<winddirection>W</winddirection>
<winddir16Point>W</winddir16Point>
<winddirDegree>275</winddirDegree>
<weatherCode>119</weatherCode>
<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png]]></weatherIconUrl>
<weatherDesc><![CDATA[Cloudy ]]></weatherDesc>
<precipMM>0.1</precipMM>
</weather>
</data>
 
M

mick

"mick" wrote in message
Another question I forgot to ask

Are 'Nodes' the same as 'Elements'?

mick
 
A

Arne Vajhøj

Been having a go at learning a bit about web services and WCF. Got that
bit working only
to get stuck at parsing the Response. I could simply parse the string
myself but I tried to
have a go at using LINQ to XML. Spent hours messing about with this last
night and still
couldn't work out how to reach particular elements and get their value.
Still as clueless
as when I started:)

If you are making web service calls and you have to parse XML in your
code, then you are doing it wrong!

So given that:

string res; //is the Response I got

var document = XElement.Parse(res)

or

var document = XDocument.Parse(res)

I'm not sure which one I'm supposed to use; any hints as to which and
(in *very* simple
terms), why?

XDocument.Parse because it is an XML document you are parsing.
What are the major benefits of using Linq as opposed to just parsing the
response string
myself? Are their any other ways?

You need to parse.

Writing your own parser would be a waste.

So you can use LINQ to XML (XDocument) or XmlDocument (or another
existing XML parser).

LINQ to XML is fine.

I would use XmLDocument and XPath, because I know and use XPath
in other languages. But that is just my preference.

Arne
 
A

Arne Vajhøj

<data>
<request>
<type>City</type>
<query>London, United Kingdom</query>
</request>
<current_condition>
<observation_time>11:11 AM</observation_time>
<temp_C>10</temp_C>
<temp_F>50</temp_F>
<weatherCode>113</weatherCode>

<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]></weatherIconUrl>

<weatherDesc><![CDATA[Sunny]]></weatherDesc>
<windspeedMiles>14</windspeedMiles>
<windspeedKmph>22</windspeedKmph>
<winddirDegree>250</winddirDegree>
<winddir16Point>WSW</winddir16Point>
<precipMM>0.0</precipMM>
<humidity>76</humidity>
<visibility>10</visibility>
<pressure>1026</pressure>
<cloudcover>25</cloudcover>
</current_condition>
<weather>
<date>2013-11-29</date>
<tempMaxC>9</tempMaxC>
<tempMaxF>48</tempMaxF>
<tempMinC>3</tempMinC>
<tempMinF>37</tempMinF>
<windspeedMiles>16</windspeedMiles>
<windspeedKmph>25</windspeedKmph>
<winddirection>W</winddirection>
<winddir16Point>W</winddir16Point>
<winddirDegree>275</winddirDegree>
<weatherCode>119</weatherCode>

<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png]]></weatherIconUrl>

<weatherDesc><![CDATA[Cloudy ]]></weatherDesc>
<precipMM>0.1</precipMM>
</weather>
</data>

XDocument doc = XDocument.Parse(data);
Given the Response below, how would I get, for example, the value of the
temp_C Element?
Console.WriteLine(doc.Root.Element("current_condition").Element("temp_C").Value);

If I wanted to get a collection of all of the Elements within the <data>
Element (that is, not
just its own child Element but every one between <data> and </data>),
how would
I do that?

foreach (XElement elm in doc.DescendantNodes().Where(node
=> node.NodeType == XmlNodeType.Element))
{
Console.WriteLine(elm.Name);
}

Arne
 
M

mick

If you are making web service calls and you have to parse XML in your
code, then you are doing it wrong!

Care to point me in the direction of what I should be doing?
XDocument.Parse because it is an XML document you are parsing.


You need to parse.

Writing your own parser would be a waste.

So you can use LINQ to XML (XDocument) or XmlDocument (or another
existing XML parser).

LINQ to XML is fine.

I would use XmLDocument and XPath, because I know and use XPath
in other languages. But that is just my preference.

I'll have to look into what XPath is, thanks.

Thanks for your reply,

mick
 
M

mick

If you are making web service calls and you have to parse XML in your
code, then you are doing it wrong!

Care to point me in the direction of what I should be doing?
XDocument.Parse because it is an XML document you are parsing.


You need to parse.

Writing your own parser would be a waste.

So you can use LINQ to XML (XDocument) or XmlDocument (or another
existing XML parser).

LINQ to XML is fine.

I would use XmLDocument and XPath, because I know and use XPath
in other languages. But that is just my preference.

I'll have to look into what XPath is, thanks.

Thanks for your reply,

mick
 
M

mick

If you are making web service calls and you have to parse XML in your
code, then you are doing it wrong!

Care to point me in the direction of what I should be doing?
XDocument.Parse because it is an XML document you are parsing.


You need to parse.

Writing your own parser would be a waste.

So you can use LINQ to XML (XDocument) or XmlDocument (or another
existing XML parser).

LINQ to XML is fine.

I would use XmLDocument and XPath, because I know and use XPath
in other languages. But that is just my preference.

I'll have to look into what XPath is, thanks.

Thanks for your reply,

mick
 
M

mick

If you are making web service calls and you have to parse XML in your
code, then you are doing it wrong!

Care to point me in the direction of what I should be doing?
XDocument.Parse because it is an XML document you are parsing.


You need to parse.

Writing your own parser would be a waste.

So you can use LINQ to XML (XDocument) or XmlDocument (or another
existing XML parser).

LINQ to XML is fine.

I would use XmLDocument and XPath, because I know and use XPath
in other languages. But that is just my preference.

I'll have to look into what XPath is, thanks.

Thanks for your reply,

mick
 
M

mick

If you are making web service calls and you have to parse XML in your
code, then you are doing it wrong!

Care to point me in the direction of what I should be doing?
XDocument.Parse because it is an XML document you are parsing.


You need to parse.

Writing your own parser would be a waste.

So you can use LINQ to XML (XDocument) or XmlDocument (or another
existing XML parser).

LINQ to XML is fine.

I would use XmLDocument and XPath, because I know and use XPath
in other languages. But that is just my preference.

I'll have to look into what XPath is, thanks.

Thanks for your reply,

mick
 
A

Arne Vajhøj

Care to point me in the direction of what I should be doing?

For SOAP/HTTP you would generate a stub from WSDL and just call that.

For POX/HTTP you would generate a XML deserializable class from XSD.

Arne
 
M

mick

For SOAP/HTTP you would generate a stub from WSDL and just call that.
For POX/HTTP you would generate a XML deserializable class from XSD.


Wow, got the XSD generated serialisable class approach to work straight away:)
I'd never heard of XSD before, nor POX for that matter (yes I had to Google
it:)).

Thanks again,
mick
 
A

Arne Vajhoej

Wow, got the XSD generated serialisable class approach to work straight
away:)
Super.

:)

I'd never heard of XSD before, nor POX for that matter (yes I had to
Google it:)).

:)

POX is not a real official term, but may people use it inspired by the
POJO and POCO terms.

Arne
 
R

RayLopez99

"mick" wrote in message



Another question I forgot to ask

A good resource on XML is Pro .NET 2.0 XML (Expert's Voice in .NET) [Bipin Joshi] , which you can get for "free" at the usual hacker sites.

I would post here such a hacker site I use to get all my CompSci books, that's found in Russia, but, like a good fishing hole, if I post it here there's a chance it will be shut down by the authorities a week later. But if you search enough you'll find it or another one. Or you can buy the book atAmazon.com (as I actually have, since I often prefer paper when studying atopic).

RL
 
Top