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"?>
<Products>
  <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" />
</Products>

A parsing script in XmlDocument: xmlparser1.ps1

# xmlparser1.ps1
[System.Xml.XmlDocument] $xdoc = new-object System.Xml.XmlDocument
$xfile = resolve-path("products.xml")
$xdoc.load($xfile)
write-host "`nreading from products.xml`n"
$rows = $xdoc.selectnodes("/Products/row") # XPath is case sensitive
$rowcount=0
foreach ($row in $rows) {
  $ProdId = $row.getAttribute("ProdId")
  $ProdName = $row.getAttribute("ProdName")
  write-host "Product ID = $ProdId Name = $ProdName"
  $rowcount++
}
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"?>
<books>
 <book> 
   <author>Carson</author>
   <price>31.95</price>
   <pubdate>05/01/2001</pubdate>
 </book>
 <pubinfo>
   <publisher>MSPress</publisher>
   <state>WA</state>
 </pubinfo>
</books>

A powershell script: xmlreader1.ps1

##  
# xmlreader1.ps1  
## 
$PWD=Get-Location
$xmlfile=$PWD.Path+"\books.xml"

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

# Parse the XML document.  
$result=$reader.Read()  
$reader.ReadStartElement("books")

# start book node
"The following is from book:"
$reader.ReadStartElement("book")

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

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

# end book node
""
$reader.ReadEndElement()

# start pubinfo node
"The following is from pubinfo:"
$reader.ReadStartElement("pubinfo")

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

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

$reader.ReadEndElement() 
# end pubinfo node

$reader.ReadEndElement()
# 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)

System.Data.dll
System.Data.OracleClient.dll
System.Data.DataSetExtensions.dll
System.Data.Design.dll
System.Data.SqlXml.dll
System.Data.Linq.dll
System.Data.SqlServerCe.dll

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:
http://blogs.msdn.com/b/microsoft_press/archive/2012/08/20/free-ebook-programming-windows-8-apps-with-html-css-and-javascript-second-preview.aspx