【问题标题】:Parse XML to ListView Windows Metro将 XML 解析为 ListView Windows Metro
【发布时间】:2015-01-13 15:52:39
【问题描述】:

我正在为 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;
}

【问题讨论】:

    标签: xml xaml silverlight windows-phone-7 windows-phone-8


    【解决方案1】:

    您需要列出您在尝试通过外部源加载数据时使用的代码。如果您尚未更改此设置,请尝试以下选项。

    尝试改变

    XDocument loadedData = XDocument.Load(peopleXMLPath);
    

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

    【讨论】:

    • 再次出错。它给了我“System.dll 中发生'System.Net.WebException' 类型的异常,但未在用户代码中处理”:/
    • 编辑:我修好了。谢谢。
    • 编辑 2:不幸的是,它在本地网络上给了我错误。我试过这个:w3schools.com/xml/note.xml 它适用于外部但不适用于我的 XML 文件。 :/无论如何,谢谢。 :)
    • 这个IP地址好像是宽带路由器的IP地址。您将遇到各种各样的问题,试图让您的模拟器解决它。我通常只是将此类文件托管为具有 xml mime 类型的 azure blob 进行测试。
    • 我想使用的逻辑是这样的......我从 SQL Server 数据库生成 XML 文件。然后我想将 XML 数据复制到我的应用程序上的本地数据库,例如 SQLite。更多或喜欢同步。这就是我使用本地IP的原因。为了让应用程序连接到这个本地 IP,平板电脑将通过 VPN 连接。我不希望数据在公共领域。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多