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)

public DataRow GetRowFromElement (
	XmlElement e
)
public DataRow GetRowFromElement (
	XmlElement e
)
public function GetRowFromElement (
	e : XmlElement
) : DataRow
Not applicable.

Parameters

e

The XmlElement whose associated DataRow you wish to retrieve.

Return Value

The DataRow containing a representation of the XmlElement; a null reference (Nothing in Visual Basic) if there is no DataRow associated with the XmlElement.

The following example modifies the price of the first book.

using System;
using System.Data;
using System.Xml;
public class Sample {
    public static void Main() {
        // Create an XmlDataDocument.
        XmlDataDocument doc = new XmlDataDocument();
        
        // Load the schema file.
        doc.DataSet.ReadXmlSchema("store.xsd");
        
        // Load the XML data.
        doc.Load("2books.xml");
        
        //Change the price on the first book.
        XmlElement root = doc.DocumentElement;
        DataRow row = doc.GetRowFromElement((XmlElement)root.FirstChild);
        row["price"] = "12.95";
        
        Console.WriteLine("Display the modified XML data...");
        Console.WriteLine(doc.DocumentElement.OuterXml);
    }
} // End class

import System.*;
import System.Data.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        // Create an XmlDataDocument.
        XmlDataDocument doc = new XmlDataDocument();
        // Load the schema file.
        doc.get_DataSet().ReadXmlSchema("store.xsd");
        // Load the XML data.
        doc.Load("2books.xml");
        //Change the price on the first book.
        XmlElement root = doc.get_DocumentElement();
        DataRow row = doc.GetRowFromElement((XmlElement)root.get_FirstChild());
        row.set_Item("price", "12.95");
        Console.WriteLine("Display the modified XML data...");
        Console.WriteLine(doc.get_DocumentElement().get_OuterXml());
    } //main
} //Sample 

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>

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft