private static void WriteXML()
{
XmlWriter writer = null;
try
{
//XmlWriterSettings settings = new XmlWriterSettings();
//settings.Indent = true;
//writer.Settings = settings;
writer = XmlWriter.Create(@"c:\abc.xml");
writer.WriteComment("sample XML fragment");
// Write an element (this one is the root).
writer.WriteStartElement("book");
// Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");
// Write the genre attribute.
writer.WriteAttributeString("genre", "novel");
// Write the title.
writer.WriteStartElement("title");
writer.WriteString("The Handmaid's Tale");
writer.WriteEndElement();
// Write the price.
writer.WriteElementString("price", "19.95");
// Lookup the prefix and write the ISBN element.
string prefix = writer.LookupPrefix("urn:samples");
writer.WriteStartElement(prefix, "ISBN", "urn:samples");
writer.WriteString("1-861003-78");
writer.WriteEndElement();
// Write the style element (shows a different way to handle prefixes).
writer.WriteElementString("style", "urn:samples", "hardcover");
writer.WriteElementString("price", "19.95");
// Write the close tag for the root element.
writer.WriteEndElement();
// Write the XML to file and close the writer.
writer.Flush();
writer.Close();
}
finally
{
if (writer != null)
writer.Close();
}
}
private static void ReadXML1()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\booksort.xml");
XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;
nodeList = root.SelectNodes("descendant::book[title='Emma']");
//Change the price on the books.
foreach (XmlNode book in nodeList)
{
book.LastChild.InnerText = "15.95";
}
nodeList = root.SelectNodes("descendant::music[price='10']");
}
private static void ReadXML()
{
//XmlTextReader reader;
//reader = new XmlTextReader(@"c:\test.xml");
XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
XmlReader reader;
reader = XmlReader.Create(@"c:\test.xml", settings);
//reader.MoveToContent();
//reader.WhitespaceHandling = WhitespaceHandling.None;
// Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.Write(reader.Value);
break;
case XmlNodeType.CDATA:
Console.Write("<![CDATA[{0}]]>", reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.Write("<?{0} {1}?>", reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
Console.Write("<!--{0}-->", reader.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
break;
case XmlNodeType.EntityReference:
Console.Write(reader.Name);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", reader.Name);
break;
}
}
}
{
XmlWriter writer = null;
try
{
//XmlWriterSettings settings = new XmlWriterSettings();
//settings.Indent = true;
//writer.Settings = settings;
writer = XmlWriter.Create(@"c:\abc.xml");
writer.WriteComment("sample XML fragment");
// Write an element (this one is the root).
writer.WriteStartElement("book");
// Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");
// Write the genre attribute.
writer.WriteAttributeString("genre", "novel");
// Write the title.
writer.WriteStartElement("title");
writer.WriteString("The Handmaid's Tale");
writer.WriteEndElement();
// Write the price.
writer.WriteElementString("price", "19.95");
// Lookup the prefix and write the ISBN element.
string prefix = writer.LookupPrefix("urn:samples");
writer.WriteStartElement(prefix, "ISBN", "urn:samples");
writer.WriteString("1-861003-78");
writer.WriteEndElement();
// Write the style element (shows a different way to handle prefixes).
writer.WriteElementString("style", "urn:samples", "hardcover");
writer.WriteElementString("price", "19.95");
// Write the close tag for the root element.
writer.WriteEndElement();
// Write the XML to file and close the writer.
writer.Flush();
writer.Close();
}
finally
{
if (writer != null)
writer.Close();
}
}
private static void ReadXML1()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\booksort.xml");
XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;
nodeList = root.SelectNodes("descendant::book[title='Emma']");
//Change the price on the books.
foreach (XmlNode book in nodeList)
{
book.LastChild.InnerText = "15.95";
}
nodeList = root.SelectNodes("descendant::music[price='10']");
}
private static void ReadXML()
{
//XmlTextReader reader;
//reader = new XmlTextReader(@"c:\test.xml");
XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
XmlReader reader;
reader = XmlReader.Create(@"c:\test.xml", settings);
//reader.MoveToContent();
//reader.WhitespaceHandling = WhitespaceHandling.None;
// Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.Write(reader.Value);
break;
case XmlNodeType.CDATA:
Console.Write("<![CDATA[{0}]]>", reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.Write("<?{0} {1}?>", reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
Console.Write("<!--{0}-->", reader.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
break;
case XmlNodeType.EntityReference:
Console.Write(reader.Name);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", reader.Name);
break;
}
}
}