|
|
Line 34: |
Line 34: |
|
</inventory> |
|
</inventory> |
|
|
|
|
|
==[[Ruby]]== |
|
==[[C#]]== |
|
⚫ |
|
⚫ |
#Example taken from the REXML tutorial (http://www.germane-software.com/software/rexml/docs/tutorial.html) |
|
|
⚫ |
// Either read the xml from a string ... |
⚫ |
|
|
|
⚫ |
XReader = XmlReader.Create(new StringReader("<inventory title=... </inventory>")); |
⚫ |
|
|
|
⚫ |
// ... or read it from the file system. |
⚫ |
#create the REXML Document from the string (%q is Ruby's multiline string, everything between the two @-characters is the string) |
|
|
⚫ |
XReader = XmlReader.Create("xmlfile.xml"); |
⚫ |
|
|
|
⚫ |
// Create a XPathDocument object (which implements the IXPathNavigable interface) |
⚫ |
%q@<inventory title="OmniCorp Store #45x10^3"> |
|
|
⚫ |
// which is optimized for XPath operation. (very fast). |
⚫ |
|
|
|
⚫ |
IXPathNavigable XDocument = new XPathDocument(XReader); |
⚫ |
|
|
|
⚫ |
// Create a Navigator to navigate through the document. |
⚫ |
|
|
|
⚫ |
XPathNavigator Nav = XDocument.CreateNavigator(); |
⚫ |
|
|
|
⚫ |
Nav = Nav.SelectSingleNode("//item"); |
⚫ |
# The invisibility cream is the first <item> |
|
|
⚫ |
// Move to the first element of the selection. (if available). |
⚫ |
invisibility = XPath.first( doc, "//item" ) |
|
|
⚫ |
|
⚫ |
# Prints out all of the prices |
|
|
⚫ |
Console.WriteLine(Nav.OuterXml); // The outer xml of the first item element. |
⚫ |
XPath.each( doc, "//price") { |element| puts element.text } |
|
|
⚫ |
// Get an iterator to loop over multiple selected nodes. |
⚫ |
# Gets an array of all of the "name" elements in the document. |
|
|
⚫ |
XPathNodeIterator Iterator = XDocument.CreateNavigator().Select("//price"); |
⚫ |
names = XPath.match( doc, "//name" ) |
|
|
⚫ |
while (Iterator.MoveNext()) |
|
⚫ |
Console.WriteLine(Iterator.Current.Value); |
|
⚫ |
Iterator = XDocument.CreateNavigator().Select("//name"); |
|
⚫ |
|
|
⚫ |
List<string> NodesValues = new List<string>(); |
|
⚫ |
while (Iterator.MoveNext()) |
|
⚫ |
NodesValues.Add(Iterator.Current.Value); |
|
⚫ |
// Convert the generic list to an array and output the count of items. |
|
⚫ |
Console.WriteLine(NodesValues.ToArray().Length); |
|
|
|
|
|
==[[PHP]]== |
|
==[[PHP]]== |
Line 84: |
Line 93: |
|
} |
|
} |
|
|
|
|
|
==[[C#]]== |
|
==[[Ruby]]== |
|
⚫ |
#Example taken from the REXML tutorial (http://www.germane-software.com/software/rexml/docs/tutorial.html) |
⚫ |
|
|
|
⚫ |
|
⚫ |
// Either read the xml from a string ... |
|
|
⚫ |
|
⚫ |
XReader = XmlReader.Create(new StringReader("<inventory title=... </inventory>")); |
|
|
⚫ |
#create the REXML Document from the string (%q is Ruby's multiline string, everything between the two @-characters is the string) |
⚫ |
// ... or read it from the file system. |
|
|
⚫ |
|
⚫ |
XReader = XmlReader.Create("xmlfile.xml"); |
|
|
⚫ |
%q@<inventory title="OmniCorp Store #45x10^3"> |
⚫ |
// Create a XPathDocument object (which implements the IXPathNavigable interface) |
|
|
⚫ |
|
⚫ |
// which is optimized for XPath operation. (very fast). |
|
|
⚫ |
|
⚫ |
IXPathNavigable XDocument = new XPathDocument(XReader); |
|
|
⚫ |
|
⚫ |
// Create a Navigator to navigate through the document. |
|
|
⚫ |
|
⚫ |
XPathNavigator Nav = XDocument.CreateNavigator(); |
|
|
⚫ |
# The invisibility cream is the first <item> |
⚫ |
Nav = Nav.SelectSingleNode("//item"); |
|
|
⚫ |
invisibility = XPath.first( doc, "//item" ) |
⚫ |
// Move to the first element of the selection. (if available). |
|
|
⚫ |
# Prints out all of the prices |
⚫ |
|
|
|
⚫ |
XPath.each( doc, "//price") { |element| puts element.text } |
⚫ |
Console.WriteLine(Nav.OuterXml); // The outer xml of the first item element. |
|
|
⚫ |
# Gets an array of all of the "name" elements in the document. |
⚫ |
// Get an iterator to loop over multiple selected nodes. |
|
|
⚫ |
names = XPath.match( doc, "//name" ) |
⚫ |
XPathNodeIterator Iterator = XDocument.CreateNavigator().Select("//price"); |
|
⚫ |
while (Iterator.MoveNext()) |
|
⚫ |
Console.WriteLine(Iterator.Current.Value); |
|
⚫ |
Iterator = XDocument.CreateNavigator().Select("//name"); |
|
⚫ |
|
|
⚫ |
List<string> NodesValues = new List<string>(); |
|
⚫ |
while (Iterator.MoveNext()) |
|
⚫ |
NodesValues.Add(Iterator.Current.Value); |
|
⚫ |
// Convert the generic list to an array and output the count of items. |
|
⚫ |
Console.WriteLine(NodesValues.ToArray().Length); |
|