using System;
using System.IO;
using System.Xml;
public class Sample {
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
XmlNode root = doc.DocumentElement;
//Create a new node.
XmlElement elem = doc.CreateElement("price");
elem.InnerText="19.95";
//Add the node to the document.
root.InsertAfter(elem, root.FirstChild);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
}
}
Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts
Wednesday, July 23, 2008
InsertAfter
XMLAttributionCollection, Item
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' publicationdate='1997'> " +
" <title>Pride And Prejudice</title>" +
"</book>");
XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
Console.WriteLine("Display all the attributes for this book...");
for (int i=0; i < attrColl.Count; i++)
{
Console.WriteLine("{0} = {1}", attrColl.Item(i).Name, attrColl.Item(i).Value);
}
}
}
HTTP, BaseURL(Uniform Resource Indicator)
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("http://localhost/baseuri.xml");
//Display information on the attribute node. The value
//returned for BaseURI is 'http://localhost/baseuri.xml'.
XmlAttribute attr = doc.DocumentElement.Attributes[0];
Console.WriteLine("Name of the attribute: {0}", attr.Name);
Console.WriteLine("Base URI of the attribute: {0}", attr.BaseURI);
Console.WriteLine("The value of the attribute: {0}", attr.InnerText);
}
}
Attributes collection
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main(){
XmlDocument doc = new XmlDocument();
doc.LoadXml("" + ");
"" +
"
//Create an attribute collection.
XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
Console.WriteLine("Display information on each of the attributes... \r\n");
foreach (XmlAttribute attr in attrColl){
Console.Write("{0}:{1} = {2}", attr.Prefix, attr.LocalName, attr.Value);
Console.WriteLine("\t namespaceURI=" + attr.NamespaceURI);
}
}
}
XmlTextWriter & MemoryStream
private byte[] GenerateXXXXXML()
{
byte[] distBytes = new byte[1];
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
xtw.Formatting = Formatting.Indented;
xtw.WriteProcessingInstruction("xml", "version='1.0' encoding='utf-8'");
xtw.WriteStartElement("PRODValidation");
xtw.WriteAttributeString("xmlns", "http://www.XXX.com/PRODUCT/validate");
// charge type
xtw.WriteStartElement("Query");
xtw.WriteAttributeString("name", "TABLENAME");
xtw.WriteStartElement("Parameters");
xtw.WriteStartElement("Param");
xtw.WriteAttributeString("name", "");
xtw.WriteAttributeString("value", "");
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteStartElement("Result");
xtw.WriteStartElement("Status");
xtw.WriteEndElement();
xtw.WriteStartElement("AdditionalInfo");
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.Flush();
ms.Flush();
distBytes = ms.ToArray();
xtw.Close();
}
return distBytes;
}
Copy File from remote location (HTTP, https, FTP..)
string RemoteFolder = @"http://yoursite.com/YourWebFolder/";
string RemoteFile = "yourfile.txt";
string url = RemoteFolder + RemoteFile;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new
StreamReader(webResponse.GetResponseStream(),Encod ing.ASCII);
string filecontent = sr.ReadToEnd();
StreamWriter sw = new StreamWriter("c:\mynewfile.txt");
sw.Write(filecontent);
sw.Flush();
sw.Close();
sr.Close();
Tuesday, July 22, 2008
XML DOM - Samples
<?xml version='1.0'?>
<bookstore xmlns="urn:newbooks-schema">
<book genre="novel" style="hardcover">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" style="other">
<title>The Poisonwood Bible</title>
<author>
<first-name>Barbara</first-name>
<last-name>Kingsolver</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>
<bookstore xmlns="urn:newbooks-schema">
<book genre="novel" style="hardcover">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" style="other">
<title>The Poisonwood Bible</title>
<author>
<first-name>Barbara</first-name>
<last-name>Kingsolver</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("newbooks.xml");
// Create an XmlNamespaceManager to resolve the default namespace.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:newbooks-schema");
// Select and display all book titles.
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bk:bookstore/bk:book/bk:title", nsmgr);
foreach (XmlNode title in nodeList) {
Console.WriteLine(title.InnerXml);
}
}
}
CreateElement:
==============
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
//Create a new node and add it to the document.
//The text node is the content of the price element.
XmlElement elem = doc.CreateElement("price");
XmlText text = doc.CreateTextNode("19.95");
doc.DocumentElement.AppendChild(elem);
doc.DocumentElement.LastChild.AppendChild(text);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
}
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
//Create a document type node and
//add it to the document.
XmlDocumentType doctype;
doctype = doc.CreateDocumentType("book", null, null, "");
doc.AppendChild(doctype);
//Create the root element and
//add it to the document.
doc.AppendChild(doc.CreateElement("book"));
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
}
}
Contains() XPath Function in MS DOM
<?xml version="1.0"?>
<!-- This file represents a fragment of a bookstore inventory database -->
<bookstore specialty="novel">
<book>
<Title>Beginning XML</Title>
<Publisher>Wrox</Publisher>
</book>
<book>
<Title>Professional XML</Title>
<Publisher>Wrox</Publisher>
</book>
<book>
<Title>Programming ADO</Title>
<author>
<first-name>Mary</first-name>
<last-name>Bob</last-name>
</author>
<datePublished>1/1/2000</datePublished>
<Publisher>Microsoft Press</Publisher>
</book>
</bookstore>
Add References, by selecting the Microsoft XML 3.0 check box.
Dim doc As MSXML2.DOMDocument
Dim nlist As MSXML2.IXMLDOMNodeList
Dim node As MSXML2.IXMLDOMNode
Set doc = New MSXML2.DOMDocument
doc.setProperty "SelectionLanguage", "XPath"
doc.Load "c:\books.xml"
Set nlist = doc.selectNodes("//book/Title[contains(.,'ADO')]")
MsgBox "Matching Nodes : " & nlist.length
For Each node In nlist
Debug.Print node.nodeName & " : " & node.Text
Next
Source : http://support.microsoft.com/kb/304265
<!-- This file represents a fragment of a bookstore inventory database -->
<bookstore specialty="novel">
<book>
<Title>Beginning XML</Title>
<Publisher>Wrox</Publisher>
</book>
<book>
<Title>Professional XML</Title>
<Publisher>Wrox</Publisher>
</book>
<book>
<Title>Programming ADO</Title>
<author>
<first-name>Mary</first-name>
<last-name>Bob</last-name>
</author>
<datePublished>1/1/2000</datePublished>
<Publisher>Microsoft Press</Publisher>
</book>
</bookstore>
Add References, by selecting the Microsoft XML 3.0 check box.
Dim doc As MSXML2.DOMDocument
Dim nlist As MSXML2.IXMLDOMNodeList
Dim node As MSXML2.IXMLDOMNode
Set doc = New MSXML2.DOMDocument
doc.setProperty "SelectionLanguage", "XPath"
doc.Load "c:\books.xml"
Set nlist = doc.selectNodes("//book/Title[contains(.,'ADO')]")
MsgBox "Matching Nodes : " & nlist.length
For Each node In nlist
Debug.Print node.nodeName & " : " & node.Text
Next
Source : http://support.microsoft.com/kb/304265
Subscribe to:
Posts (Atom)