//Order.xml
/*
<?xml version="1.0" ?>
<ord:order xmlns:ord="http://mycompany/OrderML"
xmlns:cli="http://mycompany/ClientML">
<cli:client>
<cli:firstName>Sally</cli:firstName>
<cli:lastName>Sergeyeva</cli:lastName>
</cli:client>
<ord:orderItem itemNumber="3211"/>
<ord:orderItem itemNumber="1155"/>
</ord:order>
*/
using System;
using System.Xml;
class MainClass {
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNodeList matches = doc.GetElementsByTagName("*", "http://mycompany/OrderML");
foreach (XmlNode node in matches) {
Console.Write(node.Name + " ");
foreach (XmlAttribute attribute in node.Attributes) {
Console.Write(attribute.Value + " ");
}
}
}
}
XML-RPC
Call GetElementsByTagName to get an element
using System;
using System.Xml;
public class MainClass {
private static void Main() {
XmlDocument doc = new XmlDocument();
doc.Load("ProductCatalog.xml");
XmlNodeList prices = doc.GetElementsByTagName("productPrice");
decimal totalPrice = 0;
foreach (XmlNode price in prices) {
totalPrice += Decimal.Parse(price.ChildNodes[0].Value);
}
Console.WriteLine("Total catalog value: " + totalPrice.ToString());
}
}
Save, AppendChild, CreateXmlDeclaration, CreateElement
using System;
using System.Xml;
class MainClass {
public static void Main() {
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode productsNode = doc.CreateElement("products");
doc.AppendChild(productsNode);
XmlNode productNode = doc.CreateElement("product");
XmlAttribute productAttribute = doc.CreateAttribute("id");
productAttribute.Value = "1";
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode);
XmlNode nameNode = doc.CreateElement("productName");
nameNode.AppendChild(doc.CreateTextNode("Coffee"));
productNode.AppendChild(nameNode);
XmlNode priceNode = doc.CreateElement("productPrice");
priceNode.AppendChild(doc.CreateTextNode("0.99"));
productNode.AppendChild(priceNode);
productNode = doc.CreateElement("product");
productAttribute = doc.CreateAttribute("id");
productAttribute.Value = "2";
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode);
nameNode = doc.CreateElement("productName");
nameNode.AppendChild(doc.CreateTextNode("Tea Pot"));
productNode.AppendChild(nameNode);
priceNode = doc.CreateElement("productPrice");
priceNode.AppendChild(doc.CreateTextNode("12.99"));
productNode.AppendChild(priceNode);
doc.Save(Console.Out);
}
}
LoadXml
using System;
using System.IO;
using System.Xml;
public class BookListing {
public static void Main() {
XmlDocument doc = new XmlDocument();
String entry = "<book genre='biography'" +
" ISBN='1111111111'><title>my title</title>" +
"</book>";
doc.LoadXml(entry);
StringWriter writer = new StringWriter();
doc.Save(writer); // to StringWriter
String strXML = writer.ToString(); // to String
Console.WriteLine(strXML);
}
}
A Simple XML Example
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
public static void Main() {
XmlElement xmlBook;
XmlAttribute xmlParticipantType;
XmlElement xmlFirstName;
XmlElement xmlLastName;
XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlBooks = xmlDoc.CreateElement("Books");
xmlDoc.AppendChild(xmlBooks);
xmlBook = xmlDoc.CreateElement("Book");
xmlParticipantType = xmlDoc.CreateAttribute("type");
xmlParticipantType.InnerText = "Author";
xmlBook.Attributes.Append(xmlParticipantType);
xmlFirstName = xmlDoc.CreateElement("FirstName");
xmlFirstName.InnerText = "J";
xmlBook.AppendChild(xmlFirstName);
xmlLastName = xmlDoc.CreateElement("LastName");
xmlLastName.InnerText = "R";
xmlBook.AppendChild(xmlLastName);
xmlBooks.AppendChild(xmlBook);
xmlBook = xmlDoc.CreateElement("Book");
xmlParticipantType = xmlDoc.CreateAttribute("type");
xmlParticipantType.InnerText = "Author";
xmlBook.Attributes.Append(xmlParticipantType);
xmlFirstName = xmlDoc.CreateElement("FirstName");
xmlFirstName.InnerText = "E";
xmlBook.AppendChild(xmlFirstName);
xmlLastName = xmlDoc.CreateElement("LastName");
xmlLastName.InnerText = "B";
xmlBook.AppendChild(xmlLastName);
xmlBooks.AppendChild(xmlBook);
XmlNodeList authorsList = xmlDoc.SelectNodes("Books/Book[@type="Author"]");
foreach (XmlNode node in authorsList) {
XmlNode firstName = node.SelectSingleNode("FirstName");
XmlNode lastName = node.SelectSingleNode("LastName");
Console.WriteLine("{0} {1}", firstName, lastName);
}
}
}
XML Write: comment, start element, end element, attribute
using System;
using System.Xml;
public class TestXMLWriter{
static void Main(string[] args)
{
XmlTextWriter writer = new XmlTextWriter("test.xml", new System.Text.ASCIIEncoding());
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.WriteStartDocument();
writer.WriteComment("Comment");
writer.WriteStartElement("ElementName", "myns");
writer.WriteStartAttribute("prefix", "attrName", "myns");
writer.WriteEndAttribute();
writer.WriteElementString("ElementName", "myns", "value");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
}
Write XML document to console
using System;
using System.Xml;
public class HelloXML {
public static void Main(string [] args) {
XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.WriteStartDocument( );
writer.WriteElementString("www.kutayzorlu.com/java2s/com", "XML");
writer.WriteEndDocument( );
writer.Close( );
}
}