WCF WebHttp 服务的解析结果
我正在运行一个非常简单的 WCF 服务,它返回以下(来自基本的新项目)xml:
<ArrayOfSampleItem xmlns="http://schemas.datacontract.org/2004/07/WcfRestService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<SampleItem>
<Id>1</Id>
<StringValue>Hello</StringValue>
</SampleItem>
</ArrayOfSampleItem>
然后我在 Windows Phone 7 应用程序中使用它。结果返回正常,但我在解析 xml 时遇到问题。这是我在完成请求后在回调中使用的代码:
XDocument xmlDoc = XDocument.Parse(e.Result);
itemsFetched.ItemsSource = from item in xmlDoc.Descendants("SampleItem")
select new Product()
{
Id = item.Element("Id").Value,
StringValue = item.Element("StringValue").Value
};
当我尝试添加命名空间时,集合未填充此代码:
XNamespace web = "http://schemas.datacontract.org/2004/07/WcfRestService1";
XDocument xmlDoc = XDocument.Parse(e.Result);
itemsFetched.ItemsSource = from item in xmlDoc.Descendants(web + "SampleItem")
已找到该项目,但当它尝试获取 Id 值时,我收到 null 异常。
任何帮助将不胜感激。
I have a very simple WCF service running which returns the following (from a basic new project) xml:
<ArrayOfSampleItem xmlns="http://schemas.datacontract.org/2004/07/WcfRestService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<SampleItem>
<Id>1</Id>
<StringValue>Hello</StringValue>
</SampleItem>
</ArrayOfSampleItem>
I am then consuming this in a Windows Phone 7 app. The result is coming back fine however I'm having problems parsing the xml. This is the code I am using on the callback after completion of the request:
XDocument xmlDoc = XDocument.Parse(e.Result);
itemsFetched.ItemsSource = from item in xmlDoc.Descendants("SampleItem")
select new Product()
{
Id = item.Element("Id").Value,
StringValue = item.Element("StringValue").Value
};
The collection is not populated with this, when I try adding the namespace:
XNamespace web = "http://schemas.datacontract.org/2004/07/WcfRestService1";
XDocument xmlDoc = XDocument.Parse(e.Result);
itemsFetched.ItemsSource = from item in xmlDoc.Descendants(web + "SampleItem")
The item is found but I get a null exception when it attempts to get the Id value.
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么
xmlns="..."
将元素及其所有后代放在命名空间中,因此您需要在任何地方使用XNamespace
对象web
访问元素的位置:Well the
xmlns="..."
puts the elements and all its descendants in the namespace so you need to use yourXNamespace
objectweb
anywhere where you access elements: