XmlReader.ReadStartElement 方法
定义
检查当前节点是否为元素并将读取器推进到下一个节点。Checks that the current node is an element and advances the reader to the next node.
重载
| ReadStartElement() |
检查当前节点是否为元素并将读取器推进到下一个节点。Checks that the current node is an element and advances the reader to the next node. |
| ReadStartElement(String) |
检查当前内容节点是否为具有给定 Name 的元素并将读取器推进到下一个节点。Checks that the current content node is an element with the given Name and advances the reader to the next node. |
| ReadStartElement(String, String) |
检查当前内容节点是否为具有给定 LocalName 和 NamespaceURI 的元素并将读取器推进到下一个节点。Checks that the current content node is an element with the given LocalName and NamespaceURI and advances the reader to the next node. |
ReadStartElement()
检查当前节点是否为元素并将读取器推进到下一个节点。Checks that the current node is an element and advances the reader to the next node.
public:
virtual void ReadStartElement();
public virtual void ReadStartElement ();
abstract member ReadStartElement : unit -> unit
override this.ReadStartElement : unit -> unit
Public Overridable Sub ReadStartElement ()
例外
在输入流中遇到不正确的 XML。Incorrect XML was encountered in the input stream.
在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
注解
此方法调用 IsStartElement 后跟, Read 以将你置于输入流中找到的元素的内容上。This method calls IsStartElement followed by Read to position you on the content of that element found in the input stream.
另请参阅
适用于
ReadStartElement(String)
public:
virtual void ReadStartElement(System::String ^ name);
public virtual void ReadStartElement (string name);
abstract member ReadStartElement : string -> unit
override this.ReadStartElement : string -> unit
Public Overridable Sub ReadStartElement (name As String)
参数
- name
- String
元素的限定名。The qualified name of the element.
例外
在输入流中遇到不正确的 XML。Incorrect XML was encountered in the input stream.
- 或 --or-
元素的 Name 不匹配给定的 name。The Name of the element does not match the given name.
在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
示例
下面的示例显示每个元素的文本内容。The following example displays the text content of each element.
using (XmlReader reader = XmlReader.Create("book3.xml")) {
// Parse the XML document. ReadString is used to
// read the text content of the elements.
reader.Read();
reader.ReadStartElement("book");
reader.ReadStartElement("title");
Console.Write("The content of the title element: ");
Console.WriteLine(reader.ReadString());
reader.ReadEndElement();
reader.ReadStartElement("price");
Console.Write("The content of the price element: ");
Console.WriteLine(reader.ReadString());
reader.ReadEndElement();
reader.ReadEndElement();
}
Using reader As XmlReader = XmlReader.Create("book3.xml")
' Parse the XML document. ReadString is used to
' read the text content of the elements.
reader.Read()
reader.ReadStartElement("book")
reader.ReadStartElement("title")
Console.Write("The content of the title element: ")
Console.WriteLine(reader.ReadString())
reader.ReadEndElement()
reader.ReadStartElement("price")
Console.Write("The content of the price element: ")
Console.WriteLine(reader.ReadString())
reader.ReadEndElement()
reader.ReadEndElement()
End Using
该示例使用 book3.xml 文件。The example uses the book3.xml file.
<book>
<title>Pride And Prejudice</title>
<price>19.95</price>
</book>
注解
对此方法的调用对应于对的调用, IsStartElement 然后调用 Read 。A call to this method corresponds to a call to IsStartElement followed by a call to Read.
另请参阅
适用于
ReadStartElement(String, String)
检查当前内容节点是否为具有给定 LocalName 和 NamespaceURI 的元素并将读取器推进到下一个节点。Checks that the current content node is an element with the given LocalName and NamespaceURI and advances the reader to the next node.
public:
virtual void ReadStartElement(System::String ^ localname, System::String ^ ns);
public virtual void ReadStartElement (string localname, string ns);
abstract member ReadStartElement : string * string -> unit
override this.ReadStartElement : string * string -> unit
Public Overridable Sub ReadStartElement (localname As String, ns As String)
参数
- localname
- String
元素的本地名称。The local name of the element.
- ns
- String
元素的命名空间 URI。The namespace URI of the element.
例外
在输入流中遇到不正确的 XML。Incorrect XML was encountered in the input stream.
- 或 --or- 所找到元素的 LocalName 和 NamespaceURI 属性与给定的参数不匹配。The LocalName and NamespaceURI properties of the element found do not match the given arguments.
在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
注解
对此方法的调用对应于对的调用, IsStartElement 然后调用 Read 。A call to this method corresponds to a call to IsStartElement followed by a call to Read.