C# 使用 XmlReader 读取属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11105603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:29:34  来源:igfitidea点击:

Reading attribute values with XmlReader

c#xmlxmlreader

提问by weskpga

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

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

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

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!

这就是我想要的TextReader将像节点内阅读下面的值 confidentiality="F"currency="USD"等等,但它正好跳到下一个章节不读这些值!

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

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.

如何让 textreader 读取属性值?最好先打印值“货币”,然后打印其值:“F”,依此类推。

采纳答案by JDB still remembers Monica

Get a Single, Named Attribute

获取单个命名属性

Use XmlTextReader.GetAttribute (MSDN)

使用XmlTextReader.GetAttribute (MSDN)

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

One nice feature of this function: it will not cause an Exception if the attribute is not defined - it will simply return Null.

这个函数的一个很好的特性是:如果没有定义属性,它不会导致异常——它只会返回Null.

Get All the Attributes

获取所有属性

Use XmlTextReader.MoveToAttribute (MSDN)

使用XmlTextReader.MoveToAttribute (MSDN)

Use the AttributeCount property in combination with MoveToAttribute:

将 AttributeCount 属性与 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(); 

回答by ghord

You could change the loop condition a bit so that it also iterates through attributes:

您可以稍微更改循环条件,以便它也遍历属性:

while (textReader.MoveToNextAttribute() || textReader.Read())
{ 
     switch (textReader.NodeType)
     {
         case XmlNodeType.Element:
             Console.WriteLine(textReader.Name);
             Console.WriteLine(textReader.Value);
             break;
         //...
         case XmlNodeType.Attribute:
             //use textReader.Name and textReader.Value here for attribute name and value
             break;
    }
}

MoveToNextAttributemethod advances reader to the next attribute in current element or returns false if it cannot do so.

MoveToNextAttribute方法将 reader 推进到当前元素中的下一个属性,如果不能这样做,则返回 false。