473,322 Members | 1,241 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,322 software developers and data experts.

What is the right way to create xml from dataset in C#? thanks!

In my code, I use this code to create a xml string and then load it into a
xmldocument.

for(int i=0;i<sqlReader.FieldCount;i++){

rowxml+="<"+sqlReader.GetName(i)+">"+(!sqlReader.I sDBNull(i)?sqlReader.GetVa
lue(i).ToString().Replace("&","&amp;").Replace("<" ,"&lt;").Replace(">","&gt;
"):"")+"</"+sqlReader.GetName(i)+">";
}

but it can not create xml node if the return text include characters like
PDف
my question is is there a more efficient way to create xmldocument from data
records? if my way is ok, how can I filter out invalid characters and create
formed xml?

Plase help.

Thanks!
Nov 12 '05 #1
5 8292
david wrote:
my question is is there a more efficient way to create xmldocument from data
records? if my way is ok, how can I filter out invalid characters and create
formed xml?


Using FOR XML in SQLXML you can get XmlReader, which then can be used to
load XmlDocument or XPathDocument.
Alternativelky take a look at XmlDataDocument class and DataSet.

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #2
The problem with FOR XML is that I can not get exactly the xml I want, since
data are from several tables and the fields are dynamic, it is a pain to
create a dynamic explicit for xml statement, isn't it? and in some
situation, FOR XML is just not enough. I think the way I use is more
flexible. Am I right?
"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:u2*************@tk2msftngp13.phx.gbl...
david wrote:
my question is is there a more efficient way to create xmldocument from data records? if my way is ok, how can I filter out invalid characters and create formed xml?


Using FOR XML in SQLXML you can get XmlReader, which then can be used to
load XmlDocument or XPathDocument.
Alternativelky take a look at XmlDataDocument class and DataSet.

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #3
david wrote:
The problem with FOR XML is that I can not get exactly the xml I want, since
data are from several tables and the fields are dynamic, it is a pain to
create a dynamic explicit for xml statement, isn't it? and in some
situation, FOR XML is just not enough. I think the way I use is more
flexible. Am I right?


Building XML using string concatenation is not really good idea either.
Take a look at DataSet, it looks exactly what you are looking for.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #4


Yo Gents,

I think you should check the following out, let me know if it works for
you.

David

// build an Xml string using the StringBuilder
public static String BuildXml2(Int32 Reps)
{
// make sure that the StringBuilder capacity is
// large enough for the resulting text
StringBuilder oSB = new StringBuilder(Reps * 165);
oSB.Append("<Orders method=\"2\">");

for( Int32 nRep = 1; nRep<=Reps; nRep++ )
{
oSB.Append("<Order orderId=\"");
oSB.Append(nRep);
oSB.Append("\" orderDate=\"");
oSB.Append(DateTime.Now.ToString());
oSB.Append("\" customerId=\"");
oSB.Append(nRep);
oSB.Append("\" productId=\"");
oSB.Append(nRep);
oSB.Append("\" productDescription=\"");
oSB.Append("This is the product with the Id: ");
oSB.Append(nRep);
oSB.Append("\" quantity=\"");
oSB.Append(nRep);
oSB.Append("\"/>");
}
oSB.Append("</Orders>");

return oSB.ToString();
}
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #5
David Meyer wrote:
I think you should check the following out, let me know if it works for
you.


Please, David, dont' recommend such error-prone hacks to users, what's
wrong with XmlTextWriter?

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kate Luu | last post by:
I have down load some source codes from the internet and it wasn't worked right, but I'm really know what's wrong about it. May some expert help me please...I'm deeply appreciated for your help....
3
by: Kate Luu | last post by:
I have down load some source codes from the internet and it wasn't worked right, but I'm really know what's wrong about it. May some expert help me please...I'm deeply appreciated for your help....
26
by: Lasse Edsvik | last post by:
Hello I'm trying to build a simple COM+ app in vs.net using C# and i cant register it in component manager..... what more is needed than this: using System; using...
7
by: Tim Smith | last post by:
Hi, Is it possible to create a progam to mimic the following action a) drag oledbadapter to form1.cs b) select my oracle ole connection c) enter 'select * from mytable' d) create adapter for...
3
by: Ben Becker | last post by:
I am trying to build a custom crosstab type of grid where I take some items in a data grid and based on the content of the current item compared to the previous item, determine if a new row in a...
4
by: Dave | last post by:
(My apologies for posting this on two forums. I have just found out the other one was the incorrect location) I am writing a VB.NET 2003 web application to operate on my company's intranet. It...
3
by: kkao77 | last post by:
I am trying to use schema to validate the data that user sent to my service. How do I achieve that using schema? Do I give schema to the client? or do I write my own schema validation inside web...
4
by: marek zegarek | last post by:
Hello! I have strange and simple problem.... I am starting developer. I need to create webform, that will insert my data into SQL 2005 database. My idea is to make something like MS Access...
8
by: Miro | last post by:
Hi sorry for the quick question. I am on the right track but just cant seem to find what to search for in my books / online for the answer I am looking for. I have created a dataset within my...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.