Windows Phone 7 : Parsing website XML into XDocume

2019-08-04 09:14发布

问题:

I seem to be having an issue on WP7 trying to Parse XML from a website. For some reason it never populates call. No errors, the xml looks good, I am specifying the NS, but still nothing.

Am I missing something really simple here? 4 hours in and I am banging my head on the desk.

My C# skills are 2 months old, so it could be me (probably will be).

This is my code that I am using to Parse the received XML from a website...

    public void ParseCallSignData(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            string s = e.Result;

            XDocument doc;

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.DtdProcessing = DtdProcessing.Ignore;
            using (XmlReader reader = XmlReader.Create(new StringReader(s), settings))
            {
                doc = XDocument.Load(reader);
            }

            XNamespace ns = @"http://www.qrz.com";

            var calldata = from query in doc.Descendants(ns + "Callsign")
                           select new callsign
                           {
                               call = (string)query.Element(ns +"call")
                           };

And this is the XML I am trying to parse.

<?xml version="1.0" encoding="iso-8859-1" ?>
- <QRZDatabase version="1.18" xmlns="http://www.qrz.com">
  - <Callsign>
    <call>W7EIX</call>
    <dxcc>223</dxcc>
    <fname>DICK</fname>
    <name>TONITON</name>
    <country>United States</country>
    <lat>25.586910</lat>
    <lon>-95.039318</lon>
    <grid>EL29lo</grid>
    <county>Harris</county>
    <ccode>271</ccode>
    <land>England</land>
    <class>A</class>
    <codes>ETP</codes>
    <qslmgr>DIRECT PREFERRED NO SAE REQUIRED!</qslmgr>
    <u_views>1716</u_views>
    <moddate>2010-07-05 19:32:12</moddate>
    <MSA>3360</MSA>
    <AreaCode>713</AreaCode>
    <TimeZone>Central</TimeZone>
    <GMTOffset>-6</GMTOffset>
    <DST>Y</DST>
    <eqsl>1</eqsl>
    <mqsl>1</mqsl>
    <cqzone>0</cqzone>
    <ituzone>0</ituzone>
    <locref>3</locref>
    <born>1968</born>
    <lotw>1</lotw>
  </Callsign>
  - <Session>
    <Key>afff7a6dfdff36f68fffb7dfff49fc7</Key>
    <Count>18</Count>
    <SubExp>Mon Sep 19 07:00:00 2011</SubExp>
    <GMTime>Wed Aug 3 21:06:58 2011</GMTime>
    <Remark>cpu: 0.073s</Remark>
  </Session>
</QRZDatabase>

回答1:

Those '-' characters look suspect. They shouldn't be there. (...although this might be an artefact of cut'n'paste from the browser)



回答2:

3 months later, but for the ones who want to know. I used something simpler in my applications :

string s = e.Result;
// Load the document
XDocument xdoc = XDocument.Parse(xml);

// Get the datas you want using doc.Element("...").Element("...")...
// to go down your XML doc
var calldata = from query in doc.Element("Callsign")
               select new callsign
               {
                   call = query.Element("call").Value
               };

Hope it will help somebody.