Home   Cover Cover Cover Cover
 

XML

add the assembly reference System.Xml.dll when compiling these examples:
example: csc /r:System.Xml.dll ExampleX.cs

Example "XmlReader"

The following example shows how all the last names from an XML address book can be read:

addressbook.xml

/book/samples/4/Xml/XmlReaderExample.cs
using System;
using System.Xml;

namespace Kapitel4.Xml {

  public class XmlReaderExample {
    public static void Main(String[] argv) {
      XmlTextReader r = new XmlTextReader("Adressbuch.xml");
      while (r.Read()) {
        if (r.IsStartElement("Nachname")) {     // 
          r.Read();   //  Name
          Console.WriteLine(r.Value);
        }
      }
      r.Close();
    }
  }
}




Example "XmlDocument from a string"

The following example creates an XML document from a character sting:

/book/samples/4/Xml/XmlDocumentExample.cs
using System;
using System.Xml;

namespace Kapitel4.Xml {

  public class XmlDocumentExample {
    public static void Main(String[] argv) {
      XmlDocument doc = new XmlDocument();
      doc.LoadXml("" +
            "" +
            "");
      doc.Save(Console.Out);
    }
  }
}




Beispiel "XmlDocument through creation of element nodes"

We begin with the root element addressbook according to its attribute owner, as well as sub-elements for persons, names and e-mail addresses. This is achieved by the following code:

/book/samples/4/Xml/XmlDocumentExample2.cs
using System;
using System.Xml;



  public class XmlDocumentExample2 {
    public static void Main(String[] argv) {
      //----- create the root element �addressbook�
      XmlElement rootElem = doc.CreateElement("addressbook");
      rootElem.SetAttribute("owner", "1");
      doc.AppendChild(rootElem);
      //----- create a �person� element
      XmlElement person = doc.CreateElement("person");
      person.SetAttribute("id", "1");
      XmlElement e = doc.CreateElement("firstname");  // first name
      e.AppendChild(doc.CreateTextNode("Wolfgang"));
      person.AppendChild(e);
      e = doc.CreateElement("lastname");  // last name
      e.AppendChild(doc.CreateTextNode("Beer"));
      person.AppendChild(e);
      e = doc.CreateElement("email");  // e-mail address
      e.AppendChild(doc.CreateTextNode("beer@uni-linz.at"));
      person.AppendChild(e);
      doc.DocumentElement.AppendChild(person);
      //----- create further �person� elements ...
      ...
      //----- test output on the console
      doc.Save(Console.Out);
    }
  }




Beispiel "XPath query"

We begin with the root element addressbook according to its attribute owner, as well as sub-elements for persons, names and e-mail addresses. This is achieved by the following code:.

/book/samples/4/Xml/XPathExample.cs
using System;
using System.Xml;
using System.Xml.XPath;

namespace Kapitel4.Xml {

  public class XPathExample {
    public static void Main(String[] argv) {
      XmlDocument doc = new XmlDocument();
      doc.Load("addressbook.xml");
      XPathNavigator nav = doc.CreateNavigator();
      XPathExpression expr = nav.Compile("/addressbook/person[firstname='Wolfgang']/email");
      XPathNodeIterator iterator = nav.Select(expr);
      while (iterator.MoveNext())
        Console.WriteLine(iterator.Current.Value);
    }
  }
}




Beispiel "XslTransform"

The following code snippet performs the transformation into an HTML representation according to the stylesheet.

addressbook.xml
addressbook.xsl (XSL stylesheet)

/book/samples/4/Xml/XslTransformExample.cs
using System;
using System.Xml;
using System.Xml.Xsl;




public class XPathExample {
  public static void Main(String[] argv) {
    XslTransform xt = new XslTransform();
    xt.Load("addressbook.xsl");
    xt.Transform("addressbook.xml", "addressbook.html");
  }
}

show resulting HTML page