读取属性值与的XmlReader

编程入门 行业动态 更新时间:2024-10-11 15:16:12
本文介绍了读取属性值与的XmlReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有我试图从这里 读取XML文件,并具有以下代码:

I have an XML file that I'm trying to read from here, and have the following code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; namespace XML { class Program { static void Main(string[] args) { XmlTextReader textReader = new XmlTextReader("secLendingXML.cfm.xml"); while (textReader.Read()) { switch (textReader.NodeType) { case XmlNodeType.Element: Console.WriteLine(textReader.Name); Console.WriteLine(textReader.Value); break; case XmlNodeType.Text: Console.WriteLine(textReader.Value); break; case XmlNodeType.XmlDeclaration: case XmlNodeType.ProcessingInstruction: Console.WriteLine(textReader.Name + " " + textReader.Value); break; case XmlNodeType.Comment: Console.WriteLine(textReader.Value); break; case XmlNodeType.EndElement: break; } } Console.ReadLine(); } } }

中的代码在正常工作这个意义上,它的读出节点和返回的名称。但是,问题是,我想还检索节点中的数据。换句话说,当读取测试部分在首节过后,它会读取:

The code is working properly in the sense that it's reading the nodes and returning the names. But, the issue is that I'm trying to also retrieve the data within the nodes. In other words, when it reads the first section after the test section, it will read:

slnc:DataSet slnc:Group slnc:Section slnc:ActualAvailableToBorrow *** here *** slnc:oustandingLoans

这就是我想要的TextReader像 保密=F的节点内阅读下列值,货币=USD等,但它只是跳过权到下一章节不读这些值!

This is where I want the textreader to read the following values within the node like confidentiality="F", currency="USD", etc., but it just skips right to the next section without reading these values!

<slnc:actualAvailableToBorrow xmlns:slnc="www.newyorkfed/xml/schemas/SecLending" confidentiality="F" currency="USD" decimals="0" method="AA" multiplier="5" securityLendingType="AA" status="A" value="1474"/>

我如何获得的TextReader读取属性值?这将是理想的它打印值货币,然后将其值:F,等等。

How do I get the textreader to read the attribute values? It would be ideal for it to print the value "currency", and then its value: "F", and so on.

推荐答案

使用的 XmlTextReader.GetAttribute(MSDN)

case XmlNodeType.Element: Console.WriteLine(textReader.Name); Console.WriteLine(textReader.Value); Console.WriteLine(textReader.GetAttribute("currency"));

这个功能的一个很好的功能:如果没有定义的属性不会导致异常 - 它将简单地返回空。

使用 XmlTextReader.MoveToAttribute(MSDN)

使用AttributeCount属性结合MoveToAttribute:

Use the AttributeCount property in combination with MoveToAttribute:

case XmlNodeType.Element: Console.WriteLine(textReader.Name); Console.WriteLine(textReader.Value); for (int attInd = 0; attInd < textReader.AttributeCount; attInd++){ textReader.MoveToAttribute( attInd ); Console.WriteLine(textReader.Name); Console.WriteLine(textReader.Value); } textReader.MoveToElement();

更多推荐

读取属性值与的XmlReader

本文发布于:2023-11-28 16:25:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   XmlReader

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!