XmlDataDocument.GetRowFromElement(XmlElement) Method

Definition

Retrieves the DataRow associated with the specified XmlElement.

C#
public System.Data.DataRow? GetRowFromElement(System.Xml.XmlElement? e);
C#
public System.Data.DataRow GetRowFromElement(System.Xml.XmlElement e);

Parameters

e
XmlElement

The XmlElement whose associated DataRow you want to retrieve.

Returns

The DataRow containing a representation of the XmlElement; null if there is no DataRow associated with the XmlElement.

Examples

The following example modifies the price of the first book.

C#
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

The example uses the following input files:

2books.xml

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

XML

<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>

Applies to

Produkt Versioner
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1