Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从在线xml货币提要解析到wp7应用程序_Xml_Parsing_Windows Phone 7_Converter_Currency - Fatal编程技术网

如何从在线xml货币提要解析到wp7应用程序

如何从在线xml货币提要解析到wp7应用程序,xml,parsing,windows-phone-7,converter,currency,Xml,Parsing,Windows Phone 7,Converter,Currency,因此,我知道,当我测试在文本块中显示数据时,文件正在被读取,但似乎无法将xml分解为我想要的部分,即货币名称和汇率,然后将它们添加到列表中进行选择,然后执行货币计算 使用System.Xml.Linq; 迈克·斯莫尼 { 公共部分类主页:PhoneApplicationPage { //字符串名称; //双醋栗; 网络客户端myClient; //公费 //{ //公共字符串curName{get;set;} //公共双货币{get;set;} //} //建造师 公共主页 { 初始化组件; m

因此,我知道,当我测试在文本块中显示数据时,文件正在被读取,但似乎无法将xml分解为我想要的部分,即货币名称和汇率,然后将它们添加到列表中进行选择,然后执行货币计算

使用System.Xml.Linq; 迈克·斯莫尼 { 公共部分类主页:PhoneApplicationPage { //字符串名称; //双醋栗; 网络客户端myClient; //公费 //{ //公共字符串curName{get;set;} //公共双货币{get;set;} //} //建造师 公共主页 { 初始化组件; myClient=新的网络客户端; myClient.DownloadStringCompleted+=新的DownloadStringCompletedEventHandlermyClient\u DownloadStringCompleted; 字符串urlRate=http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml; myClient.downloadstringAsyncNewUrlRate; //XDocument rateDoc=XDocument.Loadnew StringReaderurlRate; //var rates=来自rateDoc.DegenantsCube中的比率 //选择新汇率 // { //curName=rate.Attributecurrency.Value, //curRate=double.Parserate.Attributerate.Value // }; } void myClient_DownloadStringCompletedobject发送方,DownloadStringCompletedEventArgs e { 如果e.Error==null { //textBlock1.Text=e.结果; XElement currencyElements=XElement.Parsee.Result; var curList=来自currencyElements.genderantsCube中的mRate 选择新的课程费率 { 货币=mRate.Elementcurrency.Value, rate=Convert.ToDoublemRate.Elementrate.Value }; fromListBox.ItemsSource=卷发列表; } 其他的 { textBlock1.Text=e.Error.ToString; } } 私有无效转换按钮\单击对象发送器,路由目标e { 双x; //字符串urlRate=http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml; //myClient.downloadstringAsyncNewUrlRate; //x=urlRate.getElementsByTagNamerate[0]; } 私有void amountTextBox\u TextChangedobject发送者,textchangedventargs e { amountTextBox.SelectAll; } } }

参考利率 欧洲中央银行 非常感谢您的任何想法或帮助
MH

你可能会遇到一个名字空间问题的组合,我会考虑一个可怕的XML问题。XML有三层称为多维数据集的节点,这使得解析变得困难

我在下面给出的代码并不是生产代码。相反,我将它呈现给您,以便您可以看到需要解析的不同元素

XDocument doc=XDocument。Load@c:\temp\xmlfile1.xml; XNamespace ns=XNamespace.Gethttp://www.gesmes.org/xml/2002-08-01; XNamespace masterNs=XNamespace.Gethttp://www.ecb.int/vocabulary/2002-08-01/eurofxref; var信封=doc.Elementns.getnameevelope; var cubeHolder=envelope.ElementmasterNs.GetNameCube; var cubes=cubehold.ElementsmasterNs.GetNameCube; cubes.ToList.ForEachcube=> { var cubecurrences=cube.genderantsmasterns.GetNameCube; cubecurrences.ToList.ForEachcurrency=> { var country=currency.Attributecurrency.Value; var率=currency.Attributerate.Value; System.Console.WriteLineCountry:{0},速率:{1},国家,速率; }; }; 顺便说一句,我发现有一种工具对解决这类问题非常有用,那就是。当你穿过结构的各个部分时,能够把它们倒出来是非常有帮助的

--编辑:根据要求,这里是简化的代码--

字典费率=新字典; XNamespace ns=XNamespace.Gethttp://www.gesmes.org/xml/2002-08-01; XNamespace masterNs=XNamespace.Gethttp://www.ecb.int/vocabulary/2002-08-01/eurofxref; doc.Elementns.GetNameEnvelope .ElementmasterNs.GetNameCube .ElementsMasters.GetNameCube .ToList.ForEachcube=> { var cubecurrences=cube.genderantsmasterns.GetNameCube; cubecurrences.ToList.ForEachcurrency=> { var country=currency.Attributecurrency.Value; var率=currency.Attributerate.Value; rates.Addcountry,decimal.Parserate; }; };
这应该对你有用

编辑;我不知道这是不是你想要的。在公共分部类主页面下创建全局变量

然后,您可以在var多维数据集中使用for-each循环结果 循环,为每种货币设置一个case,并将其指定给全局变量。下面的示例

void myClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
       if (e.Error == null)
        {

            XElement currencyElements = XElement.Parse(e.Result);

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


            var rates= currencyElements.Descendants(ns + "Cube")
               .Where(x => x.Attribute("currency") != null)
               .Select(x => new ClassRates
               {

                   curName = x.Attribute("currency").Value,
                   curRate = Convert.ToDouble(x.Attribute("rate").Value)

               });

            foreach (var result in rates)
            {

                switch (result.curName)
                {
                    case "USD":
                        usd = result.curRate;

                        break;

                    case "GBP":
                        gbp = result.curRate;
                        break;

                    default:
                        break;

                }
                textBlock1.Text = usd.ToString();//displaying on screen
            }



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


public class ClassRates
{
    public String curName { get; set; }
    public Double curRate { get; set; }
}
xaml应该是

 <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="344,6,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" />
        <ListBox ItemsSource="{Binding ClassRates}"  Foreground="Yellow"  Height="478" HorizontalAlignment="Center" Margin="2,28,-13,0" Name="listBox1" VerticalAlignment="Top" Width="431"  TabIndex="10">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock Margin="0,0,0,0" FontSize="26" HorizontalAlignment="Left" Name="tbl1" Text="{Binding curName}" VerticalAlignment="Top" />
                        <TextBlock Margin="70,0,0,0" FontSize="26" HorizontalAlignment="Left" Name="tbl2" Text="{Binding curRate}" VerticalAlignment="Top" />


                    </Grid>
                </DataTemplate>

            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

如果看不到xml,很难帮助您。另外,您在解析XML或执行列表框时遇到了问题吗?XML采用以下格式,等等,我认为解析就像解析变量一样,我不认为完整的XML链接有什么问题,如果它能很好地帮助工作,谢谢,为了执行我的计算,我需要将相应的值存储在变量中,例如double usd=1.324等。我一直在尝试currencyElements.DegenantSNS+Cube.Wherex=>x.Attributecurrency.EqualsUSD{usd=Convert.ToDoublex.Attributerate.Value};或者ifx.Attributecurrency.Value==usd{usd=x.Attributecurrency.Value;}在检查它是否为空后,需要添加一个额外的“Where”。Where=>x.Attributecurrency!=null.Wherex=>x.Attributecurrency.Value==USD.Selectx=>new ClassRates这些变量仅在currencyElements.DegenantSNS+Cube中工作。Wherex=>x.Attributecurrency!=null.Wherex=>x.Attributecurrency.Value==JPY.Selectx=>newclassrates{JPY=Convert.ToDoublex.Attributerate.Value};chunk,你知道如何让它们在外面可用吗??你想为每种货币汇率单独设置一个全局变量,而不是将它们全部显示在一个列表中吗?是的,如果可能的话,除非我可以从列表中取出十六进制汇率的值进行计算,但我无法获得显示汇率的工作,我使用下面的答案,但我也希望将费率存储在变量中,以便计算。我一直在胡思乱想你的例子,但我无法解决它,有一个简单的方法吗??谢谢,我添加了一个示例,说明如何循环使用xml并将汇率和货币保存到字典中。谢谢,但是如何在我的项目中创建文档作为文件??我有字符串xml=e。结果;XElement doc=XElement.Parsexml;但是整个块的目标是有一个nullreferenceexception,不确定您在解析文档的位置,所以最好执行XDocument.Load;如果您采用第一种方法并运行它,您可能会得到一个关于返回空值的线索。我仍然可以使用代码片段,因此如果您处理的是不同的XML,它可能与数据相关。同样,如果您能够更改XML的结构,我会这样做,因为这种结构会让我牙疼。
 <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="344,6,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" />
        <ListBox ItemsSource="{Binding ClassRates}"  Foreground="Yellow"  Height="478" HorizontalAlignment="Center" Margin="2,28,-13,0" Name="listBox1" VerticalAlignment="Top" Width="431"  TabIndex="10">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock Margin="0,0,0,0" FontSize="26" HorizontalAlignment="Left" Name="tbl1" Text="{Binding curName}" VerticalAlignment="Top" />
                        <TextBlock Margin="70,0,0,0" FontSize="26" HorizontalAlignment="Left" Name="tbl2" Text="{Binding curRate}" VerticalAlignment="Top" />


                    </Grid>
                </DataTemplate>

            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>