473,543 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2473
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.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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><Cu st 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(xml FromDatabase);

// update the price of line 1
XmlElement line =
(XmlElement)doc .DocumentElemen t.SelectSingleN ode("Item[@Line=\"1\"]");
line.SetAttribu te("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.WriteLi ne(xmlToDatabas e);

}

Marc

"Marc Gravell" <mg******@rm.co m> wrote in message
news:On******** ******@TK2MSFTN GP11.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.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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><Cu st 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(xml FromDatabase);

// update the price of line 1
XmlElement line =
(XmlElement)doc .DocumentElemen t.SelectSingleN ode("Item[@Line=\"1\"]");
line.SetAttribu te("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.WriteLi ne(xmlToDatabas e);

}

Marc

"Marc Gravell" <mg******@rm.co m> wrote in message
news:On******** ******@TK2MSFTN GP11.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.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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:
..SelectSingleN ode("Item[@Line=\"1\"]");
What is "Item[@Line=\"1\"]"
^^^^^^^^

A MSDN sample use
"descendant::bo ok[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:
.SelectSingleNo de("Item[@Line=\"1\"]");
What is "Item[@Line=\"1\"]"
^^^^^^^^

A MSDN sample use
"descendant::bo ok[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.c om> wrote:
This looks easy.... but can you explain what this is:
.SelectSingleNo de("Item[@Line=\"1\"]");
What is "Item[@Line=\"1\"]"
^^^^^^^^

A MSDN sample use
"descendant::bo ok[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.co m>
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.c om> wrote:
This looks easy.... but can you explain what this is:
.SelectSingleNo de("Item[@Line=\"1\"]");
What is "Item[@Line=\"1\"]"
^^^^^^^^

A MSDN sample use
"descendant::bo ok[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.co m>
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)

..DocumentEleme nt.SelectSingle Node("Item[@Line=\"1\"]");

piece by piece:
..DocumentEleme nt - "from the root element, aka Order" - i.e. the outer
element in the xml
..SelectSingleN ode() - 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::boo k[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><boo k
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.c om> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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><Cu st 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(xml FromDatabase);

// update the price of line 1
XmlElement line =
(XmlElement)doc .DocumentElemen t.SelectSingleN ode("Item[@Line=\"1\"]");
line.SetAttribu te("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.WriteLi ne(xmlToDatabas e);

}

Marc

"Marc Gravell" <mg******@rm.co m> wrote in message
news:On******** ******@TK2MSFTN GP11.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.c om> wrote in message
> news:11******** **************@ f14g2000cwb.goo glegroups.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:
.SelectSingleNo de("Item[@Line=\"1\"]");
What is "Item[@Line=\"1\"]"
^^^^^^^^

A MSDN sample use
"descendant::bo ok[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)

..DocumentEleme nt.SelectSingle Node("Item[@Line=\"1\"]");

piece by piece:
..DocumentEleme nt - "from the root element, aka Order" - i.e. the outer
element in the xml
..SelectSingleN ode() - 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::boo k[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><boo k
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.c om> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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><Cu st 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(xml FromDatabase);

// update the price of line 1
XmlElement line =
(XmlElement)doc .DocumentElemen t.SelectSingleN ode("Item[@Line=\"1\"]");
line.SetAttribu te("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.WriteLi ne(xmlToDatabas e);

}

Marc

"Marc Gravell" <mg******@rm.co m> wrote in message
news:On******** ******@TK2MSFTN GP11.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.c om> wrote in message
> news:11******** **************@ f14g2000cwb.goo glegroups.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:
.SelectSingleNo de("Item[@Line=\"1\"]");
What is "Item[@Line=\"1\"]"
^^^^^^^^

A MSDN sample use
"descendant::bo ok[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
3464
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 buffers. Basically, I want to be able to use all the standard read and write functions, but I want them to refer to memory locations, rather than disk...
3
4588
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 a new memory stream, filling it with the encrypted bytes then trying to read from the crypto stream object. I have used the same approach...
5
7794
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 COM control (like Microsoft WebBrowser). For each description there is one HTML file and along description text, it contains links to related...
5
3139
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. This data can be between 60MB and 100MB under normal circumstances. I write this data to the response stream. I'm noticing that calls to this...
5
12975
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 InputStream; Stream OutPutStream; DataSet1.WriteXml(InputStream);
14
11619
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 correct way to get it into a memory stream. Any help appreciated.
3
447
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 can allocate memory once and use it to create new node in list. -Thanks -Kane
0
1291
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 use this to send the msg BinaryFormatter b = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); ...
3
2934
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 directly to disk instead it writes to memory and then at the end on either close/ dispose, it will write the data to disk. I am thinking of...
5
6852
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 again in to word object. If some one has some brilliant idea please share. In a nutshell this is what I'm currently doing ByteArray --File Stream...
0
7397
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7329
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7675
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4884
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3384
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1809
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
948
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
626
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.