Export (0) Print
Expand All

XmlDataDocument.GetRowFromElement Method

Retrieves the DataRow associated with the specified XmlElement.

Namespace:  System.Xml
Assembly:  System.Data (in System.Data.dll)

'Declaration
Public Function GetRowFromElement ( _
	e As XmlElement _
) As DataRow

Parameters

e
Type: System.Xml.XmlElement

The XmlElement whose associated DataRow you want to retrieve.

Return Value

Type: System.Data.DataRow
The DataRow containing a representation of the XmlElement; Nothing if there is no DataRow associated with the XmlElement.

The following example modifies the price of the first book.

Imports System
Imports System.Data
Imports System.Xml

public class Sample

  public shared sub Main()

      'Create an XmlDataDocument. 
      Dim doc as XmlDataDocument = new XmlDataDocument()

      'Load the schema.
      doc.DataSet.ReadXmlSchema("store.xsd") 

      'Load the XML data.
      doc.Load("2books.xml")

      'Change the price on the first book. 
      Dim book as XmlElement 
      book = CType(doc.DocumentElement.FirstChild, XmlElement)
      Dim row as DataRow 
      row = doc.GetRowFromElement(book)
      row.Item("price") = "12.95"

     Console.WriteLine("Display the modified XML data...")
     Console.WriteLine(doc.DocumentElement.OuterXml)

  end sub
end class

The example uses the following input files:

2books.xml


<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

store.xsd


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book"  type="bookType"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>

 <xsd:complexType name="authorName">
  <xsd:sequence>
   <xsd:element name="first-name"  type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft