首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在解析语句wp7之外使用类变量

如何在解析语句wp7之外使用类变量
EN

Stack Overflow用户
提问于 2012-04-12 04:34:33
回答 2查看 188关注 0票数 0

我将xml解析为变量,如下所示

代码语言:javascript
复制
void myClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
         if (e.Error == null)
            {
                XElement currencyElements = XElement.Parse(e.Result);

                XNamespace gesmes = "http://www.gesmes.org/xml/2002-08-01";
                XNamespace ns = "http://www.ecb.int/vocabulary/2002-08-01/eurofxref";

                usd=Convert.ToDouble(currencyElements.Descendants(ns +"Cube")
                .Where(x=>x.Attribute("currency")!=null)             
                .Where(x => x.Attribute("currency")
                .Value == "USD")
                .Select(x => new ClassRates
                {
                    usd=Convert.ToDouble(x.Attribute("rate").Value)  
                }));


            else
            {
                textBlock1.Text = e.Error.ToString();
            }
}

这是我的类ClassRates

代码语言:javascript
复制
public class ClassRates
        {
            public String curName { get; set; }
            public double curRate { get; set; }

            public double usd, jpy, bgn, czk, dkk, 
            gbp, huf, ltl, lvl, pln, ron, sek, chf, nok,
            hrk, rub, try1, aud, brl, cad, cny, hkd, idr, 
            ils, inr, krw, mxn, myr, nzd, php, sgd, thb, zar;
        }

变量usd在用于计算的分析块之外不可用。这是因为我通过类选择了要解析的项吗?有没有什么办法呢?我试图实现一个字典/列表/数组来存储利率,但也不起作用,这里是我尝试使用字典的版本

代码语言:javascript
复制
Dictionary<string, double> rates = new Dictionary<string, double>();
                String xml = e.Result;
                XElement doc = XElement.Parse(xml);
                XNamespace ns = XNamespace.Get("http://www.gesmes.org/xml/2002-08-01");
                XNamespace masterNs = XNamespace.Get("http://www.ecb.int/vocabulary/2002-08-01/eurofxref");

                doc.Element(ns.GetName("Envelope"))
                   .Element(masterNs.GetName("Cube"))
                   .Elements(masterNs.GetName("Cube"))
                   .ToList().ForEach(cube =>
                   {

                       var cubeCurrencies = cube.Descendants(masterNs.GetName("Cube"));

                       cubeCurrencies.ToList().ForEach(currency =>
                       {
                           var country = currency.Attribute("currency").Value;
                           var rate = currency.Attribute("rate").Value;

                           rates.Add(country, double.Parse(rate));
                       });

                   });

这会产生一个我找不到的NullReferenceException。欢迎任何想法,

代码语言:javascript
复制
    <gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
  <gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
  <Cube time="2012-04-12">
    <Cube currency="USD" rate="1.3153"/>
    <Cube currency="JPY" rate="106.54"/>
    <Cube currency="BGN" rate="1.9558"/>
    <Cube currency="CZK" rate="24.803"/>
    <Cube currency="DKK" rate="7.4384"/>
    <Cube currency="GBP" rate="0.82470"/>
    <Cube currency="HUF" rate="296.90"/>
    <Cube currency="LTL" rate="3.4528"/>
    <Cube currency="LVL" rate="0.6989"/>
    <Cube currency="PLN" rate="4.1802"/>
    <Cube currency="RON" rate="4.3745"/>
    <Cube currency="SEK" rate="8.8912"/>
    <Cube currency="CHF" rate="1.2027"/>
    <Cube currency="NOK" rate="7.6100"/>
    <Cube currency="HRK" rate="7.4758"/>
    <Cube currency="RUB" rate="38.7840"/>
    <Cube currency="TRY" rate="2.3664"/>
    <Cube currency="AUD" rate="1.2645"/>
    <Cube currency="BRL" rate="2.4075"/>
    <Cube currency="CAD" rate="1.3132"/>
    <Cube currency="CNY" rate="8.2961"/>
    <Cube currency="HKD" rate="10.2128"/>
    <Cube currency="IDR" rate="12055.30"/>
    <Cube currency="ILS" rate="4.9348"/>
    <Cube currency="INR" rate="67.8550"/>
    <Cube currency="KRW" rate="1500.58"/>
    <Cube currency="MXN" rate="17.2124"/>
    <Cube currency="MYR" rate="4.0360"/>
    <Cube currency="NZD" rate="1.6001"/>
    <Cube currency="PHP" rate="56.176"/>
    <Cube currency="SGD" rate="1.6507"/>
    <Cube currency="THB" rate="40.564"/>
    <Cube currency="ZAR" rate="10.4472"/>
</Cube>
</Cube>
</gesmes:Envelope>

mh

EN

回答 2

Stack Overflow用户

发布于 2012-04-12 05:50:48

myClient_DownloadStringCompleted中创建ClassRate的本地实例并引用它,而不是在Linq查询完成时创建新实例。

此外,请重构您的解析代码,以及ClassRates。分离Linq语句,创建一个解析方法,该方法可以将currency类型作为参数,然后根据该参数执行查询。

票数 0
EN

Stack Overflow用户

发布于 2012-04-13 02:43:57

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer%28v=vs.95%29.aspx

下面是两种反序列化xml twitter提要的方法的示例。您还需要设置您的类,并可能使用[DataContract][DataMember]属性来修饰它们。

另外,如果您有时间,请查看您的web服务的json。它的内存使用量往往比xml小得多,这意味着通过网络发送的数据比xml少得多,而且很容易反序列化。

代码语言:javascript
复制
    void myClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        var tweets = ReadToObject(e.Result);
        var tweets2 = ReadToObject2(e.Result);
    }

    private static statuses ReadToObject(string xml)
    {
        var tweets = new statuses();
        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
        {
            var ser = new DataContractSerializer(typeof(statuses));
            tweets = ser.ReadObject(ms) as statuses;
        }
        return tweets;

    }

    private static statuses ReadToObject2(string xml)
    {
        XmlSerializer ser = new XmlSerializer(typeof(statuses));
        var tweets = new statuses();

        using(var stringReader = new StringReader(xml))
        {
            using (var xmlReader = new XmlTextReader(stringReader))
            {
                tweets = ser.Deserialize(xmlReader) as statuses;
            }
        }
        return tweets;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10113520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档