【问题标题】:LINQ to XML - Nothing is binding?LINQ to XML - 没有什么是绑定的?
【发布时间】:2011-08-18 19:58:14
【问题描述】:

我正在尝试从 XML 解析,但由于某种原因,在我绑定了变量的文本框中没有显示任何内容。

我已经尝试过 Xdocuemnt 或 Xelement 的各种变体,但它似乎不起作用。 XML 结构看起来相当简单,所以我不知道出了什么问题。

任何帮助将不胜感激。

编辑************

现在一切正常。感谢我们的帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;




namespace TradeMe
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            WebClient Trademe = new WebClient();
            Trademe.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Trademe_DownloadStringCompleted);
            Trademe.DownloadStringAsync(new Uri ("http://api.trademe.co.nz/v1/Search/General.xml?search_string=" + TradeSearch.Text));
        }

        void Trademe_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;

            var r = XDocument.Parse(e.Result);

            // Declare the namespace
            XNamespace ns = "http://api.trademe.co.nz/v1";
            listBox1.ItemsSource = from TM in r.Root.Descendants(ns + "Listing")
                                   select new TradeItem
                                   {
                                       //ImageSource = TM.Element(ns + "Listing").Element(ns + "PictureHref").Value,
                                       Message = TM.Element(ns + "Title").Value,
                                       UserName = TM.Element(ns + "Region").Value
                                   }; 
        }

        public class TradeItem
        {
            public string UserName { get; set; }
            public string Message { get; set; }
            public string ImageSource { get; set; }
        }




    }
}

XML 看起来像这样。

<SearchResults xmlns="http://api.trademe.co.nz/v1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <TotalCount>12723</TotalCount> 
  <Page>1</Page> 
  <PageSize>50</PageSize> 
- <List>
- <Listing>
  <ListingId>399739762</ListingId> 
  <Title>Playstation 3 320GB Slim going at $1 Reserve</Title> 
  <Category>0202-6205-6207-</Category> 
  <StartPrice>1.0000</StartPrice> 
  <StartDate>2011-08-14T22:52:28.833Z</StartDate> 
  <EndDate>2011-08-21T08:45:00Z</EndDate> 
  <ListingLength i:nil="true" /> 
  <HasGallery>true</HasGallery> 
  <MaxBidAmount>400.0000</MaxBidAmount> 
  <AsAt>2011-08-18T19:33:41.4561556Z</AsAt> 
  <CategoryPath>/Gaming/PlayStation-3/Consoles</CategoryPath> 
  <PictureHref>http://images.trademe.co.nz/photoserver/thumb/27/183787627.jpg</PictureHref> 
  <RegionId>2</RegionId> 
  <Region>Auckland</Region> 
  <BidCount>137</BidCount> 
  <IsReserveMet>true</IsReserveMet> 
  <HasReserve>true</HasReserve> 
  <NoteDate>1970-01-01T00:00:00Z</NoteDate> 
  <ReserveState>Met</ReserveState> 
  <PriceDisplay>$400.00</PriceDisplay> 
  </Listing>

【问题讨论】:

    标签: c# xml silverlight linq


    【解决方案1】:

    试试这个:

           // Declare the namespace
           XNamespace ns = "http://api.trademe.co.nz/v1";
           listBox1.ItemsSource = from TM in r.Root.Descendants(ns+"List")
                                   select new TradeItem
                                   {                                       
                                       ImageSource = TM.Element(ns+"Listing")
                                       .Element(ns+"PictureHref").Value,
                                        Message = TM.Element(ns+"PageSize").Value,
                                       UserName = TM.Element(ns+"SearchResults").Element(ns+"Page").Value
                                   }; 
    

    【讨论】:

    • 我做了一些改动,效果很好。只有在图像中不起作用的东西,我得到未处理的异常错误。有任何想法吗?我已经更新了代码
    • 是的,试试:ImageSource = TM.Element(ns + "PictureHref").Value,你有一个额外的、多余的 Element(ns+"Listing")
    【解决方案2】:

    没有任何匹配项,因为您没有指定命名空间。见the sample code at MSDN,在此重复:

    XElement root = XElement.Parse(
    @"<Root xmlns='http://www.adventure-works.com'>
        <Child>1</Child>
        <Child>2</Child>
        <Child>3</Child>
        <AnotherChild>4</AnotherChild>
        <AnotherChild>5</AnotherChild>
        <AnotherChild>6</AnotherChild>
    </Root>");
    XNamespace aw = "http://www.adventure-works.com";
    IEnumerable<XElement> c1 =
        from el in root.Elements(aw + "Child")
        select el;
    Console.WriteLine("Result set follows:");
    foreach (XElement el in c1)
        Console.WriteLine((int)el);
    Console.WriteLine("End of result set");
    

    【讨论】:

    • 你是什么意思?我已经在许多其他 XML 上使用过它,并且效果很好。为什么这次?
    • 另外,是否可以提供一个示例,说明我将如何指定此 XML 的名称空间?
    • 谢谢。我阅读了您提供的链接,但在我当前的代码中使用我提供的 XML 时仍然遇到困难。
    猜你喜欢
    • 2012-04-20
    • 2011-05-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多