String object w/ XML data

L

Looch

Hi All,

I serialized an XML document and stored it in an SQL XML column and
retrieved it as a string object using a stored procedure.

Now I have this string object that has my XML data that I want to
write to an XML document but to a memory stream, not physically to the
hardrive. Is that possible? Would anyone mind showing an example? The
end result would be opening the XML document as a page in a web site,
ultimately marked up using an XSL.

Thanks in advance.
 
N

Nicholas Paldino [.NET/C# MVP]

Looch,

If you have a string of XML, then I would just create an XmlDocument
instance and pass the string to it:

string xml = "<xml string>";

// Create and load the document.
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

Now, you say you are loading this from SQL Server. If that is the case,
and you have access to the SqlCommand, you can bypass storing it into a
string and load it directly into the document, like so:

using (SqlCommand command = <sql command generator here>)
{
// Get the xml reader.
using (XmlReader xmlReader = command.ExecuteXmlReader())
{
// Create the document.
XmlDocument doc = new XmlDocument();

// Load the document.
doc.Load(xmlReader);
}
}
 
L

Looch

Thanks for the reply Nicholas.

I'm using the following code:

sqlCmd.CommandText = query;
XmlReader xmlReader = sqlCmd.ExecuteXmlReader();
XmlDocument xdoc = new XmlDocument();
//SqlDataReader reader = sqlCmd.ExecuteReader();
while (xmlReader.Read())
{
xdoc.Load(xmlReader);
}
xmlReader.Close();
return xdoc

And gettingthis error message:

ReadElementString method can only be called on elements with simple or
empty content.

Does the reader indeed only read XML files with simple elements (a few
of mine are complex) or am I missing something?
 
J

Jon Skeet [C# MVP]

Thanks for the reply Nicholas.

I'm using the following code:

sqlCmd.CommandText = query;
XmlReader xmlReader = sqlCmd.ExecuteXmlReader();
XmlDocument xdoc = new XmlDocument();
//SqlDataReader reader = sqlCmd.ExecuteReader();
while (xmlReader.Read())
{
xdoc.Load(xmlReader);
}
xmlReader.Close();
return xdoc

And gettingthis error message:

ReadElementString method can only be called on elements with simple or
empty content.

Does the reader indeed only read XML files with simple elements (a few
of mine are complex) or am I missing something?

Why are you calling xmlReader.Read() to start with? Just call
xdoc.Load(xmlReader) without having called Read first.

Jon
 
N

Nicholas Paldino [.NET/C# MVP]

Looch,

In my example, there was no call to Read on the reader. You pass the
reader directly into the Load method, once, and only once.
 
L

Looch

Ok, I switched to the following code:

sqlCmd.CommandText = query;
XmlReader xmlReader = sqlCmd.ExecuteXmlReader();
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlReader);
return xdoc;

And still I get the same error message:

ReadElementString method can only be called on elements with simple
or
empty content.

This is the XML string I'm trying to read (shown in XML doc form to
highlight the complex elements):

<Receipt>
<Location>
<PartyID>4221</PartyID>
<OrganizationName>My Hardware Store</OrganizationName>
<Address>
<Street1>915 Parkside Walk Lane</Street1>
<City>Lawrenceville</City>
<State>GA</State>
<PostalCode>30043</PostalCode>
</Address>
</Location>
<Device>
<DeviceID>004</DeviceID>
<TransactionNumber>1244324</TransactionNumber>
</Device>
<TransactionID>ASERFI3489T5NHQW3GF89</TransactionID>
<DateTime>10/10/2007 10:40:31 AM</DateTime>
<TotalAmount>27.00</TotalAmount>
<TransactionDetail>
<Operator>
<AssociateID>777888</AssociateID>
</Operator>
<Total>
<Text>Subtotal</Text>
<Amount>25.96</Amount>
</Total>
<Total>
<Text>Total</Text>
<Amount>27.00</Amount>
<ItemCount>4</ItemCount>
</Total>
<SaleItem>
<Text>16# Nails</Text>
<POSItemID>768764</POSItemID>
<Quantity>3</Quantity>
<UnitPrice>2.99</UnitPrice>
<ExtendedAmount>8.97</ExtendedAmount>
<ListPrice>2.99</ListPrice>
</SaleItem>
<Tax>
<Text>GA 4% Sales Tax</Text>
<Amount>1.04</Amount>
<TaxableAmount>25.96</TaxableAmount>
<TaxRate>4</TaxRate>
</Tax>
<Tender>
<Text>VISA</Text>
<Amount>27.00</Amount>
<Authorization>
<ApprovalCode>60170</ApprovalCode>
</Authorization>
<Credit>
<AccountNumber></AccountNumber>
<CardExpirationDate></CardExpirationDate>
<CardHolderName></CardHolderName>
</Credit>
</Tender>
<PromotionText>
<Text>Thank you.</Text>
</PromotionText>
</TransactionDetail>
</Receipt>
 
N

Nicholas Paldino [.NET/C# MVP]

Looch,

This string looks fine, and if I put it into a text file it defintely
validates. Also, using a small test program, it doesn't give me any
problems when I load the xml into a string and generate an XmlReader from
that.

I also put this xml into an xml column on the sql server and used a
SqlCommand to get the contents from an XmlReader returned from
ExecuteXmlReader, and it worked fine.

Can you verify that this is actually the information you are getting
from SQL Server?
 
L

Looch

Thanks Nicholas I think I'm almost there...


In my Web Service I have:

[WebMethod]
public XmlDocument getIndiReceipt (string a, string b)
{
......
}

When I mouse over the 'XmlDocument' it says 'Class XmlDocument.
Represents an XmlDocument'

However when I mouse over the web service method call (i.e.
receipt.IndiReceipt(a,b)) in the client code it says that it is an
XmlNode and an error is thrown?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top