471,327 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,327 software developers and data experts.

XML memory stream

GTi
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.

Dec 15 '05 #1
8 2316
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

"GTi" <tu****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.

Dec 15 '05 #2
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" <mg******@rm.com> wrote in message
news:On**************@TK2MSFTNGP11.phx.gbl...
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

"GTi" <tu****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.


Dec 15 '05 #3
GTi

Marc Gravell wrote:
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" <mg******@rm.com> wrote in message
news:On**************@TK2MSFTNGP11.phx.gbl...
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

"GTi" <tu****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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 ?

Dec 15 '05 #4
> 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 ?


I've never used the 'descendant' notation before... I've always just given
the name of the child element I am searching for and it has worked just
fine. I've also never had to use it with xml-namespaces, so it's possible
that may require some different usage.

--
Adam Clauss
Dec 15 '05 #5
GTi <tu****@gmail.com> wrote:
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 ?


It's XPath. See http://www.w3.org/TR/xpath and
http://www.w3schools.com/xpath/default.asp for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 15 '05 #6
GTi

Jon wrote:
GTi <tu****@gmail.com> wrote:
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 ?


It's XPath. See http://www.w3.org/TR/xpath and
http://www.w3schools.com/xpath/default.asp for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


tx m8

Dec 15 '05 #7
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" <tu****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

Marc Gravell wrote:
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" <mg******@rm.com> wrote in message
news:On**************@TK2MSFTNGP11.phx.gbl...
> 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
>
> "GTi" <tu****@gmail.com> wrote in message
> news:11**********************@f14g2000cwb.googlegr oups.com...
>>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 ?

Dec 15 '05 #8
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" <tu****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

Marc Gravell wrote:
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" <mg******@rm.com> wrote in message
news:On**************@TK2MSFTNGP11.phx.gbl...
> 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
>
> "GTi" <tu****@gmail.com> wrote in message
> news:11**********************@f14g2000cwb.googlegr oups.com...
>>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 ?

Dec 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by JW | last post: by
5 posts views Thread by Tomaz Koritnik | last post: by
5 posts views Thread by SDS | last post: by
5 posts views Thread by ad | last post: by
3 posts views Thread by CSharper | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.