C#和XmlNode.InnerText [英] C# and XmlNode.InnerText

查看:101
本文介绍了C#和XmlNode.InnerText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文档,我继承该XML文档来运行报告.下面是XML文档的示例.我试图从配置文件节点中获取值("pp4.PowerProfileID").在遍历"day"的子节点时,我使用了代码

I have an XML Document that I inherited to run a report on. A sample of the XML document is below. I am trying to get the value ("pp4.PowerProfileID") out of the profile node. While iterating through the child nodes of "day" I used the code

foreach (XmlNode profileNode in dayNode.SelectNodes("profile"))
{
    string profileID = profileNode.InnerText;
    ....
}



profileID在循环的第二次迭代中接收到值"pp4.PowerProfileID.01".这不是我最初期望的值.



profileID receives the value "pp4.PowerProfileID.01" on the second iteration of the loop. This was not the value I initially expected.

<br />
<pre lang="xml"><sum><br />
  <day d="1" ><br />
    <profile mcount="1"  pmins="1440">pp1.PowerProfileID</profile><br />
    <profile mcount="0"  pmins="0" >pp4.PowerProfileID<br />
      <hour minutes="0" >.01</hour><br />
    </profile><br />
  </day><br />
</sum><br />
</pre><br />



我已经阅读了MSDN文档,并且了解到.InnerText将节点和所有子节点的值连接起来.

所以我的问题是:
为什么将DOM设计为连接.InnerText的值?我不是在争论它的作用,而是为什么它会以它的方式起作用.有人可以举个例子说明这会有所帮助吗?

在我的xml示例中,"pp4.PowerProfileID"是对象的ID.应该将其作为自己的节点吗?

最终,我需要个人资料"节点的ID/文本,而不是子小时"节点内的文本.除了将.OuterXML值与正则表达式结合使用以获取其中的文字外,还有谁能推荐一种更好的方法?

感谢您的任何想法!

Hogan



I have read the MSDN documentation and understand that .InnerText concatenates the value of the node and all child nodes.

So my questions are:
Why would the DOM it be designed to concatenate values for .InnerText? I am not arguing what it does, but why it would work the way it does. Can somebody present an example of where this would be helpful?

In my xml example "pp4.PowerProfileID" is the ID of the object. Should that have been made its own node?

Ultimately I want the ID/text of the "profile" node and not the text inside the child "hour" node. Can anyone recommend a better way than using the .OuterXML value with a regex to get the text out of it?

Thanks for any thoughts!

Hogan

推荐答案

您可能正在寻找的可能是InnerXml (它将返回您的期望值).


---------

根据我们通过评论进行的讨论,您可以尝试以下操作:

What you may be looking for may be InnerXml (which will return what you expect).


---------

Based on our discussion via comments, here''s something you could try:

var node = profileNode.ChildNodes.Cast<XmlNode>().Where(n => n.Name == "#text").FirstOrDefault();
if (node != null)
{
    string s = node.Value;
}


所有正确. InnerText可能非常有用.实际上,这是从所有XML标签清除的纯文本.想象一下表示XHTML的XML. InnerText(如果它的Document节点将为您提供纯文本表示),就好像您复制了呈现的文本并粘贴到某些文本编辑器中一样.

怎么了?

同样重要的是,应该对任何子节点执行此操作. InnerXml将为您提供格式良好的XML,您可以将其另存为单独的文档,而InnerText将立即为您提供此内部XML的纯文本表示.

.NET的DOM实现的错误设计?我不这么认为.首先,它符合良好实施的标准.当然,性能并不是最好的,但是对于性能目标,您可以使用XmlReader/XmlWriter.

您没有解释如何处理XML.无论如何,您已经在实例XmlDocument中全面解析了数据-学习使用它.您可以得到一些帮助,但前提是您要解释自己的最终目标.
All correct. InnerText can be very useful. Effectively, this is pure text cleaned from all XML Tags. Imagine XML representing XHTML. The InnerText if its Document node will give you pure text representation, pretty much as if you copied the rendered text and pasted in some text editor.

What''s wrong with that?

Also important, that this should be done with any child node. InnerXml will give you well-formed XML that you could save as a separate document, and InnerText will immediately give you a pure-text representation of this inner XML.

Bad design of DOM implementation of .NET? I don''t think so. First of all, it conforms to standards, well implemented. Of course, the performance is not the best, but for performance goals you can use XmlReader/XmlWriter.

You did not explain how you want to process your XML. Anyway, you have comprehensively parsed data in your instance XmlDocument -- learn to use it. You can get some help, but only if your explain your ultimate goals.


如果最终要寻找小时元素的值,只需获取子节点的InnerText即可.暂时获取的节点数.
If you''re ultimately looking for the value of the hour element, just get the InnerText for the child node instead of the node you''re getting it for now.


这篇关于C#和XmlNode.InnerText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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