Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create an xml file and stored at hard drive

Status
Not open for further replies.

Raul2005

Programmer
Sep 23, 2005
44
US
The next code display a dataset in an xml format. Could some one provide a technique to create an xml file out of this.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace XMLBond_Hub
{
class Program
{
static void Main()
{

SqlConnection connString = new SqlConnection("Server=test1;UID=usuario;PWD=pass;Database=xxx");
SqlDataAdapter objDA = new SqlDataAdapter("select First_Name,Last_Name,Email,Company_Name from contact ", connString);
DataSet ds = new DataSet();
objDA.Fill(ds);
string xml = ds.GetXml();

}
}
}

Thanks
 
Something like this, maybe.
Code:
StreamWriter sw = new StreamWriter("c:\\myfile.xml");
sw.Write(xml);
sw.Flush();
sw.Close();
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for the support I obtained this xml file.
There is a way to go from this XML1 to XML2
(XML 1)
<NewDataSet>
<Table>
<First_Name>Terry</First_Name>
<Last_Name>Adams</Last_Name>
<Email>adams@hotmail</Email>
<Company_Name>COMPANY1</Company_Name>
<Address_2>203rd Floor</Address_2>
<City>North Caroline</City>
<State_>NC</State_>
<Zip>07011</Zip>
<Country>United States</Country>
<Phone>+1 (202) 000-8888</Phone>
<Fax>+1 (202) 000-8888</Fax>
<position>Mr.</position>
</Table>
<Table>
<First_Name>Pedro</First_Name>
<Last_Name>Roca</Last_Name>
<Email>roca@hotmail</Email>
<Company_Name>COMPANY2</Company_Name>
<Address_2>20 rd Floor</Address_2>
<City>New Jersey</City>
<State_>NC</State_>
<Zip>07011</Zip>
<Country>United States</Country>
<Phone>+1 (202) 000-8888</Phone>
<Fax>+1 (202) 000-8888</Fax>
<position>Mr.</position>
</Table>
</NewDataSet>


(xml 2)


<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE channel SYSTEM 'CORRER.dtd'>
<channel channelType="admin">
<dealer value="D10" />
<channel_id>000000</channel_id>
<admin adminId="AAAAAA" feedType="Users">

<person>
<first_name>Pedro</first_name>
<last_name>Torres</last_name>
<email type="Corporate">ptorres@test.com</email>
<company>Test Company Inc.</company>
<address>
<street>21 AVE.</street><street>2 floor</street>
<city>NEW JERSEY</city>
<state>NJ</state>
<zip>07033</zip>
<country value="USA" />
</address>
<phone>(201)555-0000</phone>
<fax>1324567890</fax>
<position>Developer</position>
</person>

</admin>


Thanks


 
Yes, you would write an XSLT to transform it, then use C# code to call the transformation.

Transformation code might look something like:
Code:
public string Transform(string sXMLDocument, string sXSLFile)
{
  XmlDocument xd = new XmlDocument(); 
  xd.Load(sXMLDocument);
  XPathNavigator xdNav = xd.CreateNavigator();
  XslTransform tr = new XslTransform();
  
  tr.Load(sXSLFile);
  StringWriter sw = new StringWriter();
  tr.Transform(xdNav,null,sw);
  return sw.ToString();
}
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
chiph, thats the same way I write out XML files, however I never used the sw.Flush() method, what does this line do? And should I add it to my XML output code?
 
Typically, when you write data out to a file using a StreamWriter, it actually writes to a memory buffer. This reduces the lag created by disk writing.

flush() makes the text in the memory buffer write to disk. It's essentially forcing the writer to ACTUALLY write to the disk.

You should put it at the end of your write xml method.
 
The thing is, I've been writting files to disk for a long time without using the Flush() method, and never had a problem. So I'm assuming the Close() method forces a flush before it actually closes. So calling a flush right before close seems redundant. I dunno, seems rather insiginificat either way, I guess I just want to know if the Flush method is SAFER than just closing it.
 
It may be redundant, but it's definitely safer.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top