all groups > dotnet xml > august 2003 >
You're in the

dotnet xml

group:

Creating one xml doc from mutliple strings selected from database


Creating one xml doc from mutliple strings selected from database Kathy Burke
8/25/2003 12:48:59 PM
dotnet xml:
Hi, tried this a few weeks ago, but still need some help.

I have a simple database table, fields: Customer, WorkOrder, xmlDoc.

The xmlDoc fields contains a small blob of xml data, such as:

<WorkOrder>
<ProductID/>
<OrderDate/>
<ProductGroup>
<SerialNo/>
<SerialNo/>
<SerialNo/>
</ProductGroup>
</WorkOrder>

Based on a SQL query such as SELECT xmlDoc WHERE Customer = "xyz", I
need to:

1) Create a new xmlDocument with only a root element such as <NewQuery>.

2) Bring each xmlDoc blob into the new xmlDocument within the NewQuery
root.

3) Remove all the ?xml version="1.0"? processing instructions except for
the first one so that the new xmlDocument is well formed...or remove
them all then just add one back?

Does anyone have an example I could see for something like this...or
describe what I need to actually do.

Thanks.
Kathy

*** Sent via Developersdex http://www.developersdex.com ***
Re: Creating one xml doc from mutliple strings selected from database Anthony Christianson
8/25/2003 5:58:54 PM
This should do it



XmlDocument xmlDoc = new XmlDocument();
xmlDoc.AppendChild( xmlDoc.CreateElement("NewQuery"));

XmlDocument xmlBlob = new XmlDocument();
xmlBlob.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"
?><WorkOrder><ProductID/><OrderDate/><ProductGroup><SerialNo/><SerialNo/><Se
rialNo/></ProductGroup></WorkOrder>");

xmlDoc.DocumentElement.AppendChild( xmlDoc.ImportNode(
xmlBlob.DocumentElement, true));

xmlBlob = new XmlDocument();
xmlBlob.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<WorkOrder><ProductID/><OrderDate/><ProductGroup><SerialNo/><SerialNo/><Seri
alNo/></ProductGroup></WorkOrder>");


xmlDoc.DocumentElement.AppendChild( xmlDoc.ImportNode(
xmlBlob.DocumentElement, true));



[quoted text, click to view]

Re: Creating one xml doc from mutliple strings selected from database Kathy Burke
8/26/2003 5:30:02 AM
Thanks, Anthony!

I understand some of it. Don't understand how I work my sql query into
it, and how I don't get the repeating ?xml? pi...I assume I need to
create the new xmlDocument, then do a for each structure to add each
result of my query? Say, I end up with a dataset that has ten xmlDoc
results. How would I work that in. Sorry, I'm not very bright these
days...I think I use to be...until xml/xsl came along!

Thanks again.

Kathy

*** Sent via Developersdex http://www.developersdex.com ***
Re: Creating one xml doc from mutliple strings selected from database Anthony Christianson
8/26/2003 8:40:22 AM
If the blobs are coming from the database you could do something like this:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.AppendChild( xmlDoc.CreateElement("NewQuery"));

using( SqlConnection conn = new SqlConnection("conectionstring")){
SqlCommand cmd = conn.CreateCommand();
cmd.CommecdText = "sql query";
cmd.CommadType= CommandType.StoredProcedure;
IDataReader reader = cmd.ExecuteReader();
while( reader.Read()){
XmlDocmument xmlBlob = new XmlDocument();
xmlBlob.LoadXml( reader.GetString(0)); //Assuming the Xml Blob
is the first field.
xmlDoc.DocumentElement.AppendChild(
xmlDoc.ImportNode(xmlBlob.DocumentElement, true)); //By Importing the
Document Element, it skips the ?xml? Directives
}
}

By using the DocumentElement of the XmlDocument object, you are skipping all
of the ?xml? directives.
ImportNode allows you to import a node from one document to another.



[quoted text, click to view]

Re: Creating one xml doc from mutliple strings selected from database Kathy Burke
8/26/2003 11:42:01 AM
Anthony, just what I needed.

Many thanks.

Kathy

*** Sent via Developersdex http://www.developersdex.com ***
AddThis Social Bookmark Button