在C#中使用XElement读取XML [英] Read the XML using XElement in C#

查看:463
本文介绍了在C#中使用XElement读取XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i我在c#中使用xml

i希望使用XElement读取xml

我的Xml文件是这样的

Hi

i am working with xml in c#
i want to read the xml using XElement
My Xml file is like this

<?xml version="1.0" encoding="UTF-8"?><!--XML GENERATED by IntuitDataSyncEngine (IDS) using \\SBDomainServices\CDM\branches\3.9.0-rel-1-->
<RestResponse xmlns="http://www.intuit.com/sb/cdm/v2"
xmlns:xdb          ="http://xmlns.oracle.com/xdb"
xmlns:xsi          ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.intuit.com/sb/cdm/v2 ../common/RestDataFilter.xsd">
<Items>
<Item>
<Name>139</Name>
<Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
<Item>
<Name>139</Name>
<Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
</Item>
<Item>
<Name>139</Name>
<Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
</Item>
</RestResponse>





来自这个xml我希望阅读物品

< RestResponse> 中节点我可以轻松地读取项目

但是< RestResponse>我无法使用XElement阅读项目

如何阅读项目..?说一些答案



from this xml i want the read the items
without "<RestResponse>" node i can easily read the item
but with "<RestResponse>" i can''t read the item using XElement
How to read the Items ..? say some Answers

推荐答案

请检查以下链接



http://www.dotnetperls.com/xelement [ ^ ]

http://msdn.microsoft .com / zh-CN / library / bb348975.aspx [ ^ ]
Please check following links

http://www.dotnetperls.com/xelement[^]
http://msdn.microsoft.com/en-us/library/bb348975.aspx[^]


您好,



看看这里:

http://www.mssqltips.com/sqlservertip/1524 / reading-xml-documents-using-linq-to-xml / [ ^ ]

LINQ to XML [ ^ ]

http: //broadcast.oreilly.com/2010/10/understanding-c-simple-linq-to.html [ ^ ]

http://www.c-sharpcorner.com/UploadFile/mahesh/xLinkDoc06202007130827PM/xLinkDoc.aspx [ ^ ]

LINQ to XML简介 [ ^ ]

阅读XML使用LINQ的文档 [ ^ ]
Hi,

Have a look here:
http://www.mssqltips.com/sqlservertip/1524/reading-xml-documents-using-linq-to-xml/[^]
LINQ to XML[^]
http://broadcast.oreilly.com/2010/10/understanding-c-simple-linq-to.html[^]
http://www.c-sharpcorner.com/UploadFile/mahesh/xLinkDoc06202007130827PM/xLinkDoc.aspx[^]
An Introduction to LINQ to XML[^]
Reading XML documents using LINQ[^]


这是无效的XML。

< Items>在每个< Item>之前打开元素。元素,但每次之后都不会关闭。

打开3次,只关闭一次。

我猜它应该只打开一次,如同< Item>的容器元素。



编辑:我看到你更新了XML示例。



首先: XDocument 不允许评论成为顶级元素(应该合法),所以它仍然无法解析这个。将评论移到< RestResponse> 元素中可修复此问题。



真正的问题是由于命名空间。 < RestResponse> 标记中的xmlns属性定义了无前缀名称空间。因此,在引用元素名称或按元素名称过滤时必须使用该名称空间。

这显示了如何(它假设Root元素与 Item 元素,即无前缀):

This is not valid XML.
The <Items> element is opened before each <Item> element, but it isn''t closed after each.
Opened 3 times, closed only once.
I''d guess that it should be opened only once, as the container for the <Item> elements.

Edited: I see you updated the example XML.

First: XDocument does not allow the comment to be a top-level element (it should, it is legal), so it will still fail to parse this. Moving the comment to be inside the <RestResponse> element fixes this.

The real issue is due to namespaces. The xmlns attribute in the <RestResponse> tag defines the no-prefix namespace. So that namespace must be used when referring to or filtering by element names.
This shows how (it assumes that the Root element is in the same namespace as the Item elements, i.e. no prefix):
// InputXML is the XML as a string. Use XDocument.Load() for file/stream input.
XDocument source = XDocument.Parse(InputXML);
XName itemName = XName.Get("Item", source.Root.Name.NamespaceName);
foreach (XElement item in source.Descendants(itemName))
{
  Console.WriteLine(item.ToString());
}



此输出:


This outputs:

<Item xmlns="http://www.intuit.com/sb/cdm/v2">
  <Name>139</Name>
  <Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
</Item>
<Item xmlns="http://www.intuit.com/sb/cdm/v2">
  <Name>139</Name>
  <Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
</Item>
<Item xmlns="http://www.intuit.com/sb/cdm/v2">
  <Name>139</Name>
  <Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
</Item>



请注意输出中的显式命名空间属性。


Note the explicit namespace attribute in the output.


这篇关于在C#中使用XElement读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆