解析 XML 到 ListView Windows Metro

Parse XML to ListView Windows Metro

我正在为 windows 8.1 开发应用程序。我能够将 XML 文件解析为 ListView。它是本地 XML 文件,但是当我将 XML 路径 (XMLFile1.xml) 作为 URL(例如 http://192.168.x.x/test.xml)时,它给了我一个错误。 这是我的代码。

XAML 文件:

<ListView x:Name="listView1" HorizontalAlignment="Left" Height="373" Margin="1229,264,0,0" VerticalAlignment="Top" Width="127">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="10" >
                        <TextBlock Text="{Binding FirstName}"/>
                        <TextBlock Text="{Binding LastName}"/>
                        <TextBlock Text="{Binding Age}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

XAML.cs 文件:

public class Person
        {
            string firstname;
            string lastname;
            int age;

            public string FirstName
            {
                get { return firstname; }
                set { firstname = value; }
            }

            public string LastName
            {
                get { return lastname; }
                set { lastname = value; }
            }

            public int Age
            {
                get { return age; }
                set { age = value; }
            }
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            string peopleXMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "XMLFile1.xml");
            XDocument loadedData = XDocument.Load(peopleXMLPath);

            var data = from query in loadedData.Descendants("person")
                       select new Person
                       {
                           FirstName = (string)query.Element("firstname"),
                           LastName = (string)query.Element("lastname"),
                           Age = (int)query.Element("age")
                       };
            listView1.ItemsSource = data;
}

您需要列出在尝试通过外部源加载数据时使用的代码。如果您没有更改它,请尝试下面的选项。

尝试更改

XDocument loadedData = XDocument.Load(peopleXMLPath);

XDocument loadedData = XDocument.Load("http://192.168.x.x/test.xml");