The links in Jon's post should be your starting point, and note that the SDK
that comes with MSXML4 is also a lifeline...
However - to answer your question for the 2 examples:
Example 1 (mine)
..DocumentElement.SelectSingleNode("Item[@Line=\"1\"]");
piece by piece:
..DocumentElement - "from the root element, aka Order" - i.e. the outer
element in the xml
..SelectSingleNode() - from the node in question (Order), evaluates an
expression, and returns the first matching element found (if any), else
returns null
The query is XPath; first we indicate which elements we are looking to
return (in this case the Item), then the conditions: anything in square
braces is essentially a "where" clause; @ means attribute, other element is
assumed; in English this would be "from the current node, look at the
immediate children called "Item" where they have an attribute "Line" with a
value of 1 (compared as a string)" - so the whole expression basically says
"Find the first /Order/Item with the Line attribute being 1"
Example 2 (MSDN)
descendant::book[author/last-name='Smith']
On its own, this doesn't tell me what the start point of the query is (which
is actually quite important); the double-colon here indicates an axis;
descendant is the axis that means "below this node, but at any level";
English interpretation of the query:
From the current context node (wherever that is) return all descendants
called "book" which themselves have an "author" element with a "last-name"
sub-element with the text value of "Smith"
As an example, if I evaluated the above query from the root node (somedata)
of the xml below, I would (without testing, so don't quote me) expect it to
return books 1 and 2, but not 3 (because the last-name is expressed as an
attribute, and the query only looks for elements called last-name).
<somedata>
<blah>
<book
id="1"><author><last-name>Smith</last-name><last-name>Jones</last-name></author></book>
<something><book
id="2"><author><last-name>Smith</last-name></author></book></something>
<book id="3"><author last-name="Smith"/></book></something>
</blah>
</somedata>
Marc
GTi said:
Marc said:
Very simple example (not using xml-namespaces, schema, etc):
static void Main(string[] args) {
// (your code that gets the xml as a string from a text
column) - note the escaping is obviously not part of the xml
string xmlFromDatabase = "<Order><Cust Name=\"Fred\"
ID=\"12345\"/><Item Line=\"1\" Code=\"123\" Price=\"1234.4\"/></Order>";
// create a doc an load the contents from a string
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlFromDatabase);
// update the price of line 1
XmlElement line =
(XmlElement)doc.DocumentElement.SelectSingleNode("Item[@Line=\"1\"]");
line.SetAttribute("Price", "1500.00");
// retreive the updated contents as a string
string xmlToDatabase = doc.OuterXml;
// (your code that saves the xml as a string to a text
column)
Console.WriteLine(xmlToDatabase);
}
Marc
Marc Gravell said:
Firstly, you can't really load xml "into" an xml-reader, as it doesn't
actually store the data, it it just an efficient way of reading through
xml in a firehose fashion - i.e forwards only, read only.
If you want to manipulate the data (in a more convenient than string
manipulation) then you might want to look at XmlDocument; this is an
object that holds in memory the contents of the xml and allows for
querying, enumeration, changes etc - but the rub is that it takes a lot
more memory, because a: you need to hold the contents, and b: it is
broken
into objects, rather than just being a string. If you go down this
route,
you can use the LoadXml() method to populate it with your contents
(from a
string from the db), and the OuterXml property to get the entire
contents
as a string. Depending on the size of your data, you can also do this
with
streams - the Load() method accepts (as one of the overloads) an input
stream, and the Save() method accepts an output stream.
The main warning, however: if your xml is alarmingly big (i.e. is
essentially an entire database), then the corresponding XmlDocument
will
be even bigger.
Any use?
Marc
I need to load a XML into a string. Is it possible and how do I do it?
I have a database using ODBC (MS SQL, P.SQL, Oracle.....) where the
XML
file is stored as a TEXT type. I can load this field as a string type.
But I need to load this into a "XML reader", do any changes with the
XML doc and store it again into the database.
The database stuff is OK, I only need some help to load and save a XML
as a string type.
Can someone help me out here?
I have never used XML before.
This looks easy.... but can you explain what this is:
.SelectSingleNode("Item[@Line=\"1\"]");
What is "Item[@Line=\"1\"]"
^^^^^^^^
A MSDN sample use
"descendant::book[author/last-name='Smith']"
^^^^^^^^^^^^^^^
What is the syntax for the parameter ?