passing values from text file into xml file(urgent)

G

Guest

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
 
M

Morten Wennevik

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.
 
G

Guest

Morten Wennevik said:
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.

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
 
J

Jon Skeet [C# MVP]

C# to XML said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top