Read BookStore.xml using XPath Iterator.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
namespace BookStoreApp
{
class Program
{
static void Main(string[] args)
{
Program bk = new Program();
bk.DisplayBooksLess10();
bk.DisplayNovel();
bk.DisplayFirstNameAInIt();
bk.DisplayNovelOverSix();
//Add the new book
bk.AddBookNode();
}
public void DisplayFirstNameAInIt()
{
Console.WriteLine("A char in Author's first name");
XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator NodeIter;
//String strExpression;
// Open the XML.
docNav = new XPathDocument("BookStore.xml");
nav = docNav.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("b", "http://www.contoso.com/books");
nsmgr.AddNamespace("g", "http://www.contoso.com/genre");
nsmgr.AddNamespace("a", "http://www.contoso.com/author");
XPathExpression expr = nav.Compile("/b:bookstore/b:book[contains(a:author/a:first-name,'a')]/b:title");
expr.SetContext(nsmgr);
//strExpression = "/bookstore/book/title[../price>10.00]";
NodeIter = nav.Select(expr);
while (NodeIter.MoveNext())
{
Console.WriteLine("Book Title: {0}", NodeIter.Current.Value);
};
Console.WriteLine("\n" + "Press ENTER key....");
Console.ReadLine();
}
public void DisplayNovelOverSix()
{
Console.WriteLine("Average of Novels over £6");
XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator NodeIter;
//String strExpression;
// Open the XML.
docNav = new XPathDocument("BookStore.xml");
nav = docNav.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("b", "http://www.contoso.com/books");
nsmgr.AddNamespace("g", "http://www.contoso.com/genre");
nsmgr.AddNamespace("a", "http://www.contoso.com/author");
//XPathExpression expr = nav.Compile("/b:bookstore/b:book[@g:genre='novel' and b:price>6.00]/b:title");
//expr.SetContext(nsmgr);
string avg = nav.Evaluate("sum(descendant::b:bookstore/b:book[@g:genre='novel' and b:price>6.00]/b:price)" +
" div count(descendant::b:bookstore/b:book[@g:genre='novel' and b:price>6.00])", nsmgr).ToString();
Console.WriteLine("Books Average Price: {0}", avg);
Console.WriteLine("\n" + "Press ENTER key....");
Console.ReadLine();
}
public void DisplayNovel()
{
Console.WriteLine("Display Novels");
XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator NodeIter;
//String strExpression;
// Open the XML.
docNav = new XPathDocument("BookStore.xml");
nav = docNav.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("b", "http://www.contoso.com/books");
nsmgr.AddNamespace("g", "http://www.contoso.com/genre");
XPathExpression expr = nav.Compile("/b:bookstore/b:book[@g:genre='novel']/b:title");
expr.SetContext(nsmgr);
//strExpression = "/bookstore/book/title[../price>10.00]";
NodeIter = nav.Select(expr);
while (NodeIter.MoveNext())
{
Console.WriteLine("Book Title: {0}", NodeIter.Current.Value);
};
Console.WriteLine("\n" + "Press ENTER key....");
Console.ReadLine();
}
public void DisplayBooksLess10()
{
Console.WriteLine("Books less than £10");
XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator NodeIter;
//String strExpression;
// Open the XML.
docNav = new XPathDocument("BookStore.xml");
nav = docNav.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("b", "http://www.contoso.com/books");
nsmgr.AddNamespace("g", "http://www.contoso.com/genre");
nsmgr.AddNamespace("a", "http://www.contoso.com/author");
XPathExpression expr = nav.Compile("/b:bookstore/b:book[b:price<'10.00']/b:title");
expr.SetContext(nsmgr);
//strExpression = "/bookstore/book/title[../price>10.00]";
NodeIter = nav.Select(expr);
while (NodeIter.MoveNext())
{
Console.WriteLine("Book Title: {0}", NodeIter.Current.Value);
};
Console.WriteLine("\n" + "Press ENTER key....");
Console.ReadLine();
}
public void AddBookNode()
{
Console.WriteLine("Adding a book node to existing xml");
XmlDocument myDoc = new XmlDocument();
myDoc.Load("BookStore.xml");
XmlNode root = myDoc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(myDoc.NameTable);
nsmgr.AddNamespace("b", "http://www.contoso.com/books");
nsmgr.AddNamespace("g", "http://www.contoso.com/genre");
nsmgr.AddNamespace("a", "http://www.contoso.com/author");
// Create a Book element and populate its attributes
System.Xml.XmlElement newBook = myDoc.CreateElement("book", "http://www.contoso.com/books");
//create the three attributes to hold the values
newBook.SetAttribute("genre", "http://www.contoso.com/genre", "novel");
newBook.SetAttribute("publicationdate", "2011-06-14");
newBook.SetAttribute("ISBN", "1-1245-45455");
System.Xml.XmlElement xeTitle = myDoc.CreateElement("title");
xeTitle.InnerText = "MyBook";
// Insert the new element under the node we created
newBook.AppendChild(xeTitle);
System.Xml.XmlElement myAuthor = myDoc.CreateElement("author");
myAuthor.SetAttribute("xmlns", ("http://www.contoso.com/author"));
System.Xml.XmlElement xeFirstname = myDoc.CreateElement("first-name");
xeFirstname.InnerText = "Siraj";
myAuthor.AppendChild(xeFirstname);
System.Xml.XmlElement xeLastname = myDoc.CreateElement("last-name");
xeLastname.InnerText = "Zarook";
myAuthor.AppendChild(xeLastname);
newBook.AppendChild(myAuthor);
// Price
System.Xml.XmlElement xePrice = myDoc.CreateElement("price");
xePrice.InnerText = "1.47";
// Insert the new element under the node we created
newBook.AppendChild(xePrice);
//append the whole node to file
myDoc.DocumentElement.AppendChild(newBook);
myDoc.Save("BookStoreModified.xml");
Console.WriteLine("File BookStoreModified.xml has been created in the application folder"
+ "\n" + "Press ENTER key..");
Console.ReadLine();
}
}
}
Monday, 25 July 2011
Saturday, 25 June 2011
Monday, 13 June 2011
Read XML File
'xmldoc.Load("C:\Siraj\" & "Response_Test.xml")
'Dim Response_data As String
Dim nsmRequest As New XmlNamespaceManager(xmldoc.NameTable)
nsmRequest.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/")
nsmRequest.AddNamespace("oug", "http://webcomponent.components.oug.osgi.scorex.com/")
'XmlElement root = xmldoc.DocumentElement;
Dim nodes As XmlNodeList = xmldoc.DocumentElement.SelectNodes("//oug:MyWSResponse/items/item", nsmRequest)
' you cannot use prefix with NameSpaceMng
'XmlNodeList nodes = xmldoc.DocumentElement.SelectNodes("//myWSResponse/items");
For Each node As XmlNode In nodes
'Response.Write(node.InnerText)
If node.Name.ToUpper = "ITEM" And node.InnerText.ToUpper = "MY_RC" Then
'
If node.NextSibling.InnerText = "0" Then
SubmissionSuccess = True
End If
ElseIf node.Name.ToUpper = "ITEM" And node.InnerText.ToUpper = "RC_DESC" Then ' useful when WF_RC IS <> 0
RC_DESC = node.NextSibling.InnerText
ElseIf node.Name.ToUpper = "ITEM" And node.InnerText.ToUpper = "CODE" Then ' useful when WF_RC IS <> 0
' This could be a lookup table - ALL SYS???? Error codes
Return_Code = node.NextSibling.InnerText
If Return_Code <> "0" Then
SubmissionSuccess = False
Exit For ' If error during submission data tag will not be used
End If
ElseIf node.Name.ToUpper = "ITEM" And node.InnerText.ToUpper = "DATA" Then
Response_Data = node.NextSibling.InnerText
End If
Next
*******Select Single Node
thisNode = xmlResponseData.DocumentElement.SelectSingleNode("//OUTPUT/MY_NUM")
If Not thisNode Is Nothing Then
AppNum = thisNode.InnerText
End If
'Dim Response_data As String
Dim nsmRequest As New XmlNamespaceManager(xmldoc.NameTable)
nsmRequest.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/")
nsmRequest.AddNamespace("oug", "http://webcomponent.components.oug.osgi.scorex.com/")
'XmlElement root = xmldoc.DocumentElement;
Dim nodes As XmlNodeList = xmldoc.DocumentElement.SelectNodes("//oug:MyWSResponse/items/item", nsmRequest)
' you cannot use prefix with NameSpaceMng
'XmlNodeList nodes = xmldoc.DocumentElement.SelectNodes("//myWSResponse/items");
For Each node As XmlNode In nodes
'Response.Write(node.InnerText)
If node.Name.ToUpper = "ITEM" And node.InnerText.ToUpper = "MY_RC" Then
'
If node.NextSibling.InnerText = "0" Then
SubmissionSuccess = True
End If
ElseIf node.Name.ToUpper = "ITEM" And node.InnerText.ToUpper = "RC_DESC" Then ' useful when WF_RC IS <> 0
RC_DESC = node.NextSibling.InnerText
ElseIf node.Name.ToUpper = "ITEM" And node.InnerText.ToUpper = "CODE" Then ' useful when WF_RC IS <> 0
' This could be a lookup table - ALL SYS???? Error codes
Return_Code = node.NextSibling.InnerText
If Return_Code <> "0" Then
SubmissionSuccess = False
Exit For ' If error during submission data tag will not be used
End If
ElseIf node.Name.ToUpper = "ITEM" And node.InnerText.ToUpper = "DATA" Then
Response_Data = node.NextSibling.InnerText
End If
Next
*******Select Single Node
thisNode = xmlResponseData.DocumentElement.SelectSingleNode("//OUTPUT/MY_NUM")
If Not thisNode Is Nothing Then
AppNum = thisNode.InnerText
End If
Reading XML Files with ASP.NET
This is full article from Faizal Khan.
Reading XML Files with ASP.NET
Read This
Read This
Reading XML Files with ASP.NET
We can bind an XML file to a list/combo box control.
Read This
Populate with ListArray and Hashtable/
Read This
Sunday, 12 June 2011
Display XmlDataSource. using XPath ASP.Net
The following code example demonstrates how to use an XmlDataSource control with a templated Repeater control to display XML data that has been filtered using an XPath expression.
**************
Displaying XML Data in a List Control
Creating Advertisements
MSDN Source
**************
Walkthrough: Creating a Web Page to Display XML Data
Displaying XML Data in a List Control
Walkthrough: Displaying and Tracking Advertisements with the AdRotator Control
Creating Advertisements
Tuesday, 31 May 2011
create xml doc in vb.net
Public Function next_ten_dates(ByVal startDate As Date) As String
Dim xDoc As New XmlDocument
Dim xNode As XmlElement
Dim newAtt As XmlAttribute
Dim xDocRoot As XmlElement = xDoc.CreateElement("dates")
xDoc.AppendChild(xDocRoot)
For i As Integer = 0 To 10
Dim xdate As XmlNode = xDoc.CreateElement("date")
xdate.InnerText = getJapaneseDate(startDate.AddDays(i))
newAtt = xDoc.CreateAttribute("daysinmonth")
Dim datesinmonth = startDate.DaysInMonth(startDate.Year, _
startDate.Month)
newAtt.Value = datesinmonth.ToString()
xdate.Attributes.Append(newAtt)
xDocRoot.AppendChild(xdate)
Next
Dim XMLString As String = ""
Dim sw As New StringWriter()
Dim xw As New XmlTextWriter(sw)
xDoc.WriteTo(xw)
XMLString = sw.ToString()
next_ten_dates = XMLString
End Function
Dim xDoc As New XmlDocument
Dim xNode As XmlElement
Dim newAtt As XmlAttribute
Dim xDocRoot As XmlElement = xDoc.CreateElement("dates")
xDoc.AppendChild(xDocRoot)
For i As Integer = 0 To 10
Dim xdate As XmlNode = xDoc.CreateElement("date")
xdate.InnerText = getJapaneseDate(startDate.AddDays(i))
newAtt = xDoc.CreateAttribute("daysinmonth")
Dim datesinmonth = startDate.DaysInMonth(startDate.Year, _
startDate.Month)
newAtt.Value = datesinmonth.ToString()
xdate.Attributes.Append(newAtt)
xDocRoot.AppendChild(xdate)
Next
Dim XMLString As String = ""
Dim sw As New StringWriter()
Dim xw As New XmlTextWriter(sw)
xDoc.WriteTo(xw)
XMLString = sw.ToString()
next_ten_dates = XMLString
End Function
Subscribe to:
Posts (Atom)