如何使用LINQ to XML查询XML文件 [英] How to query an XML file using LINQ to XML

查看:93
本文介绍了如何使用LINQ to XML查询XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从给定的项目文件中提取所有条件编译constsants。该文件有几个 PropertyGroup 元素,如下所示:

 <   PropertyGroup    条件  = '$(配置)| $(平台)'=='调试| AnyCPU' >  
< DebugSymbols > true < / DebugSymbols >
< DebugType > 完整< / DebugType >
< 优化 > false < /优化 >
< OutputPath > bin \Debug \ < ; / OutputPath >
< DefineConstants > DEBUG; TRACE < / DefineConstants >
< ErrorReport > 提示< / ErrorRepo rt >
< WarningLevel > 4 < / WarningLevel >
< / PropertyGroup >



我第一次尝试Linq到Sql都给了我所有节点或者没有。我可以运行什么查询:



(a)获取所有 DefineConstants 元素,但包含其父元素查询,因为我需要条件属性,或

(b)获取所有 PropertyGroup 具有 DefineConstants 子元素的元素并包含这些子元素。



我正在尝试构建条件编译字典常量,所以下次遇到像 DEVIL_FROG_POTATO_GREEN 这样的常量时,你可以弹出我的字典窗口并阅读或希望写下它为别人做的事情。

解决方案

(配置)|


(平台)'=='调试| AnyCPU' >
< DebugSymbols > true < / DebugSymbols >
< DebugType > 完整< / DebugType >
< 优化 > false < /优化 >
< OutputPath > bin \Debug \ < / OutputPath >
< DefineConstants > DE BUG; TRACE < / DefineConstants >
< ErrorReport > prompt < / ErrorReport >
< WarningLevel > 4 < / WarningLevel >
< / PropertyGroup >



我第一次尝试Linq到Sql都给了我所有节点,或者没有。我可以运行什么查询:



(a)获取所有 DefineConstants 元素,但包含其父元素查询,因为我需要条件属性,或

(b)获取所有 PropertyGroup 具有 DefineConstants 子元素的元素并包含这些子元素。



我正在尝试构建条件编译字典常量,所以下次你遇到像 DEVIL_FROG_POTATO_GREEN 这样的常量时,你可以弹出我的字典窗口并阅读或希望写下它为别人做的事情。


对于Linq到xml,你需要添加以下名称空间

1. System.Xml

2. System.Xml.Linq



我们有从xml(paranet节点和子节点)查询所需节点的选项数量

请查看以下链接以获取相同的内容/>


https://msdn.microsoft.com/en-us/library/bb943906.aspx [ ^ ]



http://www.dotnetcurry.com/showarticle.aspx?ID=564 [ ^ ]





< a href =http://www.codeproject.com/Articles/169598/Parse-XML-Documents-by-XMLDocument-and-XDocument>按XML文档和XDocument解析XML文档 [ ^ ]







通过XMLDocument和XDocument解析XML文档 [ ^ ]





LINQ to XML [ ^ ]



http://codesamplez.com/programming/linq-to-xml-tutorial [< a href =http://codesamplez.com/programming/linq-to-xml-tutorialtarget =_ blanktitle =New Window> ^ ]


I am trying to extract all conditional compilation constsants from a given project file. The file has several PropertyGroup elements like this:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
	<DebugSymbols>true</DebugSymbols>
	<DebugType>full</DebugType>
	<Optimize>false</Optimize>
	<OutputPath>bin\Debug\</OutputPath>
	<DefineConstants>DEBUG;TRACE</DefineConstants>
	<ErrorReport>prompt</ErrorReport>
	<WarningLevel>4</WarningLevel>
</PropertyGroup>


My first attempts at Linq to Sql all gave me all nodes, or none. What queries can I run to:

(a) get all DefineConstants elements, but include their parent element in the query, because I need the Condition attribute, or
(b) get all PropertyGroup elements that have DefineConstants children and include this children.

I am trying to build a dictionary of conditional compilation constants, so next time you come across a constant like DEVIL_FROG_POTATO_GREEN, you can just pop open my dictionary window and read, or hopefully, write what it does for others.

解决方案

(Configuration)|


(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup>


My first attempts at Linq to Sql all gave me all nodes, or none. What queries can I run to:

(a) get all DefineConstants elements, but include their parent element in the query, because I need the Condition attribute, or
(b) get all PropertyGroup elements that have DefineConstants children and include this children.

I am trying to build a dictionary of conditional compilation constants, so next time you come across a constant like DEVIL_FROG_POTATO_GREEN, you can just pop open my dictionary window and read, or hopefully, write what it does for others.


For Linq to xml you need to add following name space
1. System.Xml
2. System.Xml.Linq

we have number of option to query desired node from the xml ( paranet node and child node )
Please check on following links for the same

https://msdn.microsoft.com/en-us/library/bb943906.aspx[^]

http://www.dotnetcurry.com/showarticle.aspx?ID=564[^]


Parse XML Documents by XMLDocument and XDocument[^]



Parse XML Documents by XMLDocument and XDocument[^]


LINQ to XML[^]

http://codesamplez.com/programming/linq-to-xml-tutorial[^]


这篇关于如何使用LINQ to XML查询XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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