简体   繁体   English

如何使用C#确定XML文档的属性计数

[英]How to determine Attributes count for XML documents using C#

I'm using XmlReader and xml navigator as following: 我正在使用XmlReader和xml导航器,如下所示:

XmlDocument doc = new XmlDocument();
doc.Load(txtFileName.Text.Trim());
// Create the navigator.

XPathNavigator xnav = doc.CreateNavigator();

Once you created the navigation you can select the descendant or children and you can determine the number of childrens. 创建导航后,您可以选择后代或子代,也可以确定子代数。 However, i have problem which is when i need to know the number of attributes of that node i can't for example if you want to know the number of childrens you use the following code. 但是,我有一个问题,那就是当我需要知道该节点的属性数量时,例如,如果您想知道子节点的数量,则无法使用以下代码。

int size = xnav.SelectDescendants(XPathNodeType.All, true).Count;

This function accepts two parameters in this case: the type of Nodes you want to be selected like text comment or element and the second parameter is set to true if you want the root of this subtree which is the current node to be selected as well as a part of the subtree. 在这种情况下,此函数接受两个参数:要选择的节点类型(如文本注释或元素),如果要选择此子树的根(即当前节点),则第二个参数设置为true子树的一部分。

The result of select[*] is XPathIterator object. select[*]的结果是XPathIterator对象。

Of course you don't want the count of comments or white space in general so you don't use the all attribute to retrieve all the node but you need to specify the type of nodes you want lets say we need all elements, attributes and text therefore we should run the following command: 当然,您通常不需要注释数或空格,因此您不需要使用all属性来检索所有节点,但是需要指定想要的节点类型,可以说我们需要所有元素,属性和因此,我们应该运行以下命令:

int size = xnav.SelectDescendants(XPathNodeType.Attribute | XPathNodeType.Element | XPathNodeType.Text , true).Count;

i think that you need the value of the attribute to be counted as well so you have to double the result of attributes count more over it is not working to specify the type of Nodes like this so i made the following: 我认为您也需要对属性值进行计数,因此您必须将属性计数的结果加倍,因为它不能像这样指定节点的类型,因此我做了以下工作:

int size = (xnav.SelectDescendants(XPathNodeType.Attribute, true).Count * 2) +
    xnav.SelectDescendants(XPathNodeType.Element, true).Count +
    xnav.SelectDescendants(XPathNodeType.Text , true).Count;

Currently i have the count of all the above except the attribute i have no count for that. 目前,我除了属性我没有上述所有计数。

HEEELLLPPP!!! 哎呀! please

ps i don't want to go through a loop to elements it is very costly to do so. PS:我不想遍历元素,这样做非常昂贵。

If you are really worried about performance, you should use the XmlReader to do this directly, and not be doing all of these XPath queries. 如果您真的担心性能,则应使用XmlReader直接执行此操作,而不要执行所有这些XPath查询。 It would also have the benefit that you could track all of your counts at once, rather than performing several document-wide queries. 这也将带来好处,您可以一次跟踪所有计数,而不用执行多个文档范围的查询。

I will do it manually, from string 我将手动从字符串开始

<img src="" style="" width=""/>

Just count the "=" sign and that's it 只需计算“ =”符号即可

You could use xpath to find out how many attributes there are 您可以使用xpath找出有多少个属性

XmlDocument doc = new XmlDocument();
doc.Load(txtFileName.Text.Trim());
XPathNavigator xnav = doc.CreateNavigator();
xnav.Evaluate("count(//@*)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM