By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

passing values from text file into xml file(urgent)

P: n/a
Hi Friends,

I am new to csharp , I am using the following code to write into xml file :

Just go through the code :

fp = File.OpenText(Server.MapPath(".\\upload\\") + "test.txt");

string info = fp.ReadToEnd();

string[] arInfo = new string[4];

// define which character is seperating fields
char[] splitter = {'|','^'};

arInfo = info.Split(splitter);

for(int x = 0; x < arInfo.Length; x++)
{
Response.Write(arInfo[x] + "<br>");
a = arInfo[x];
XmlTextWriter xwriter = new XmlTextWriter("C:/xml/data.xml",
System.Text.Encoding.UTF8);
xwriter.WriteStartDocument(true);
xwriter.WriteStartElement("LogIn");
xwriter.WriteElementString("username", arInfo[0]);
xwriter.WriteElementString("password",arInfo[1]);
xwriter.WriteElementString("third", arInfo[2]);
xwriter.WriteEndElement();
xwriter.WriteEndDocument();
xwriter.Close();
Label3.Text = "File Succesfully Read!";

}

Here I am creating new XML file and Elements to insert a values:

What i need is I have specify the location of particular XML file (this XML
File i have created manually ,i have created elements in this schemas)

I have to insert each values into xml file element,(i.e without specifying
each element )

Thanx and Regards
Jul 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi.

A couple of tips. When using StreamReader or anything with Close() or Dispose() implemented, remember to Close or Dipose when you are done using the object. Better yet, use the 'using' statement

using(StreamReader sr = File.Open(...))
{
string text = sr.ReadToEnd();
}

Close/Dispose will be called automatically when the block ends.

In your case you just want the text from the file.
If you use .Net 2.0 simply do

string info = File.GetAllText(Server.MapPath(".\\upload\\") + "test.txt");

Remember to consider the Encoding. Unless specified text will be read as if it is UTF8 encoded.
--
Happy coding!
Morten Wennevik [C# MVP]
Jul 24 '06 #2

P: n/a


"Morten Wennevik" wrote:
Hi.

A couple of tips. When using StreamReader or anything with Close() or Dispose() implemented, remember to Close or Dipose when you are done using the object. Better yet, use the 'using' statement

using(StreamReader sr = File.Open(...))
{
string text = sr.ReadToEnd();
}

Close/Dispose will be called automatically when the block ends.

In your case you just want the text from the file.
If you use .Net 2.0 simply do

string info = File.GetAllText(Server.MapPath(".\\upload\\") + "test.txt");

Remember to consider the Encoding. Unless specified text will be read as if it is UTF8 encoded.
--
Happy coding!
Morten Wennevik [C# MVP]
Hi Morten ,
Reading file is mot a problem for me,

I have to insert into xml file,these each text which i read from text file

I dnt want to use new XML file , I have to insert into existing xml file
segments

Can u briefly explain me how to do..

THanx and regards
>
Jul 25 '06 #3

P: n/a
C# to XML <Ct****@discussions.microsoft.comwrote:
Reading file is mot a problem for me,

I have to insert into xml file,these each text which i read from text file

I dnt want to use new XML file , I have to insert into existing xml file
segments

Can u briefly explain me how to do..
Load the XML file using XmlDocument.Load. Find where you want to insert
things, possibly using XPath. Create new nodes and use AppendChild to
add them into the document at the right place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.