473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 2457
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
3
by: JW | last post by:
I can encrypt the contents of a memory stream with no problems, however when decrypting( I'm using the same key an IV ) I get an exception stating bad data. I'm at a lost. I'm actually creating...
5
by: Tomaz Koritnik | last post by:
Hi I have many short HTML files stored in a binary stream storage to display descriptions for various items in application. HTML would be display inside application using some .NET control or...
5
by: SDS | last post by:
I am writing an ASP.NET application (in C#) that, as part of a particular response, populates a MemoryStream object with binary data that is being collected from a Process object's StandardOutput. ...
5
by: ad | last post by:
I used use SharpZipLib to compress files in disk. But now I want to compress stream into another stream in memory(the stream not associated with disk file) My pseudo is: Stream...
14
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the...
3
by: Kane | last post by:
When you create node 1 you allocate memory and link it Again when you create node 2 you would allocate memory for that node in a different section of the code. Is there more efficient way where I...
0
by: phreak008 | last post by:
I'm using SendMessage(...) to send a message to all other process that might run. It works well. My problem is when I try to pass data using shared memory. Here is my code. In the 1st process, I...
3
by: CSharper | last post by:
I have a resource file that i open to write.After writing almost 300MB I call the close method and It failed with out of memory. One thing I learned that, in C#, resource writers doesn't write data...
5
by: Nitin Mahajan | last post by:
Guys Is there a way in C# to create a word object directly from a memory stream without passing that to hard disk (file stream). I think it doesn't makes sense to create a file just to read it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.