Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / December 2003

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
david - 10 Dec 2003 09:06 GMT
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.IsDBNull(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
?P???D????????

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!
Oleg Tkachenko - 10 Dec 2003 10:47 GMT
> 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.

Signature

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

david - 10 Dec 2003 16:20 GMT
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?

> > 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
[quoted text clipped - 3 lines]
> load XmlDocument or XPathDocument.
> Alternativelky take a look at XmlDataDocument class and DataSet.
Oleg Tkachenko - 10 Dec 2003 16:51 GMT
> 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.
Signature

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

David Meyer - 23 Dec 2003 20:24 GMT
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();
}
Oleg Tkachenko - 24 Dec 2003 07:50 GMT
> 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?

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.