Ebook: The Art of XSD SQL Server XML Schema by Jacob Sebastian

From XSD fundamental knowledges, design principles to SQL server XSD implementation, Jacob Sebastian provides the in-depth details in this online book (http://assets.red-gate.com/community/books/assets/the-art-of-xsd.pdf). Check it out! 


Process XML Using DOM Model in Powershell

The XML Document Object Model (DOM) treats XML data as a standard set of objects and is used to process XML data in memory. The System.Xml namespace provides a programmatic representation of XML documents, fragments, nodes, or node-sets. It is based on the World Wide Web Consortium (W3C) DOM Level 1 Core and the DOM Level 2 Core recommendations.

The XmlDocument class represents an XML document. It includes members for retrieving and creating all other XML objects. Using the XmlDocument, and its related classes, you can construct XML documents, load and access data, modify data, and save changes.

The XML Document Object Model (DOM) class is an in-memory representation of an XML document. The DOM allows you to programmatically read, manipulate, and modify an XML document. The XmlReader class also reads XML; however, it provides non-cached, forward-only, read-only access.

I wrote another script in the powershell, but used XmlDocument this time.

A sample XML input file : products.xml

I just generated from MS SQL 2008 using select * from Products for xml raw TYPE,ROOT(‘Products”)

<?xml version="1.0"?>
  <row ProdId="1" ProdName="BOND" />
  <row ProdId="2" ProdName="EQUITY" />
  <row ProdId="3" ProdName="FX" />
  <row ProdId="4" ProdName="MUTUAL FUND" />
  <row ProdId="5" ProdName="ETF" />
  <row ProdId="6" ProdName="OPTION" />
  <row ProdId="7" ProdName="INSURANCE" />
  <row ProdId="8" ProdName="DEPOSIT" />

A parsing script in XmlDocument: xmlparser1.ps1

# xmlparser1.ps1
[System.Xml.XmlDocument] $xdoc = new-object System.Xml.XmlDocument
$xfile = resolve-path("products.xml")
write-host "`nreading from products.xml`n"
$rows = $xdoc.selectnodes("/Products/row") # XPath is case sensitive
foreach ($row in $rows) {
  $ProdId = $row.getAttribute("ProdId")
  $ProdName = $row.getAttribute("ProdName")
  write-host "Product ID = $ProdId Name = $ProdName"
write-host "`Total Rows: $rowcount"

Execution Results

PS C:\henry416\> .\xmlparser1.ps1
reading from products.xml
Product ID = 1 Name = BOND
Product ID = 2 Name = EQUITY
Product ID = 3 Name = FX
Product ID = 4 Name = MUTUAL FUND
Product ID = 5 Name = ETF
Product ID = 6 Name = OPTION
Product ID = 7 Name = INSURANCE
Product ID = 8 Name = DEPOSIT
Total Rows: 8

Reading XML with the XmlReader in Powershell

The XmlReader class in .Net Framework reads XML data from a stream or file. It provides non-cached, forward-only, read-only access to XML data. I wrote a simple program in powershell to explore XmlReader class as belows. Have fun!

A sample XML input file: books.xml

<?xml version="1.0"?>

A powershell script: xmlreader1.ps1

# xmlreader1.ps1  

# Create XML Reader  
$reader = [system.Xml.XmlReader]::Create($xmlfile) 

# Parse the XML document.  

# start book node
"The following is from book:"

# Read and Display auther  
"author: {0}" -f $reader.ReadString()  

# Read and display price  
"price: {0}" -f $reader.ReadString()  
# Read and display pubdate
"pubdate: {0}" -f $reader.ReadString()  

# end book node

# start pubinfo node
"The following is from pubinfo:"

# Read and Display publisher  
"publisher: {0}" -f $reader.ReadString()  

# Read and display state  
"state: {0}" -f 

# end pubinfo node

# End of Script 

Execution Result

PS C:\henry416\> .\xmlreader1.ps1

The following is from book:
author: Carson
price: 31.95
pubdate: 05/01/2001

The following is from pubinfo:
publisher: MSPress
state: WA

What exactly is ADO.NET?

ADO.NET consists of the following assemblies (On my machine, they locate in C:\WINNT\Microsoft.NET\assembly)


Free ebook: Programming Windows 8 Apps with HTML, CSS, and JavaScript (Second Preview)

Kudos to Microsoft Press, the second review edition of ebook Programming Windows 8 Apps with HTML, CSS, and JavaScript, by Kraig Brockschmidt can be found here for free: