XML and ADO.NET


This topic almost doesn't merit a section of its own, as only one class, XmlDataDocument, needs to be examined, and XmlDataDocument inherits from XmlDocument. What am I trying to get at? To use ADO.NET and XML together, you need to create a DataSet (see Chapter 12) and create an XmlDataDocument with it. Then you can manipulate the database data just as you did with XmlDocument.

The XmlDataDocument class adds properties and members to streamline some activities and to make them more "relational database"-like, but other than that you have already learned what you need to work with XML originating from an ADO.NET database:

  • DataSet is the DataSet used to create the XmlDataDocument.

  • CreateEntityReference() is a method that is not supported and throws an exception.

  • GetElementById() is a method that is not supported and throws an exception.

  • GetElementFromRow() gets an XmlElement associated with a specified DataRow.

  • GetRowFromElement() gets a DataRow associated with a specified XmlElement.

  • Load() loads into the XmlDocument using a filename, Stream, TextReader, or XmlReader, and then synchronizes with the DataSet.

The example in Listing 13-13 is an exact duplicate of Listing 13-8, except that the source of the XML data is the DCV_DB database created in Chapter 12.

Listing 13-13: Dumping the DCV_DB Database to a ListBox Using XML

start example
 void BuildListBox() {     try     {         SqlConnection *connect = new SqlConnection(); #ifdef SQLAuth         //  SQL Server authentication         connect->ConnectionString =             S"User ID=sa; Password=;"             S"Data Source=(local); Initial Catalog=DCV_DB;"; #else         //  Windows Integrated Security         connect->ConnectionString =             S"Persist Security Info=False; Integrated Security=SSPI;"             S"Data Source=(local); Initial Catalog=DCV_DB;"; #endif         SqlDataAdapter *dAdapt = new SqlDataAdapter();         DataSet *dSet           = new DataSet();         dAdapt->SelectCommand  =             new SqlCommand(S"SELECT * FROM Authors", connect);         dAdapt->Fill(dSet, S"Authors");         XmlDataDocument *doc = new XmlDataDocument(dSet);         Navigate(doc->DocumentElement, 0);     }     catch (Exception *e)     {         MessageBox::Show(e->Message, S"Navigate Aborted");     } } void Navigate(XmlNode *node, Int32 depth) {     if (node == 0)         return;     Output->Items->Add(String::Format(         S"{0}: Name='{1}' Value='{2}'",         String::Concat(indent(depth),__box(node->NodeType)->ToString()),         node->Name, node->Value));     if (node->Attributes != 0)     {         for (Int32 i = 0; i < node->Attributes->Count; i++)         {             Output->Items->Add(String::Format(                 S"{0}Attribute: Name='{1}' Value='{2}"',                 indent(depth+1),node->Attributes->ItemOf[i]->Name,                 node->Attributes->ItemOf[i]->Value));         }     }     Navigate(node->FirstChild, depth+1);     Navigate(node->NextSibling, depth); } 
end example

As you can see, the only code that is different from the original (Listing 13-9) is the standard code to create a DataSet and then the placing of the DataSet within an XmlDataDocument. If you need a refresher on creating a DataSet, please review Chapter 12.

 SqlConnection *connect = new SqlConnection(); #ifdef SQLAuth //  SOL Server authentication connect->ConnectionString =     S"User ID=sa; Password=;"     S"Data Source=(local); Initial Catalog=DCV_DB;"; #else //  Windows Integrated Security connect->ConnectionString =     S"Persist Security Info=False; Integrated Security=SSPI;"     S"Data Source=(local); Initial Catalog=DCV_DB;"; #endif SqlDataAdapter *dAdapt = new SqlDataAdapter(); DataSet *dSet           = new DataSet(); dAdapt->SelectCommand  = new SqlCommand(S"SELECT * FROM Authors", connect); dAdapt->Fill(dSet, S"Authors"); XmlDataDocument *doc = new XmlDataDocument(dSet); 

Figure 13-13 shows the resulting ListBox dump by ADONET.exe of all the nodes and attributes that make up the DCV_DB database DOM tree.

click to expand
Figure 13-13: The ListBox dump of the DCV_DB database DOM tree




Managed C++ and. NET Development
Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
ISBN: 1590590333
EAN: 2147483647
Year: 2005
Pages: 169

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net