help with XML writing - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

help with XML writing

uriznurizn Posts: 31Member
hi im trying to update a line in an xml file. i have the following so far

[code]
static void Main(string[] args)
{
string Location = args[0];
string OldVersion = args[1];
string NewVersion = args[2];
string testymctest;
int Lineposition;

XmlTextReader reader = null;
XmlTextWriter writer = null;

try
{
reader = new XmlTextReader( Location);
writer = new XmlTextWriter( Location, System.Text.Encoding.UTF8);

while ( reader.Read())
{
testymctest = reader.Value;
if (testymctest == OldVersion)
{
//Lineposition = reader.LinePosition;
//writer.WriteString("
Console.WriteLine (testymctest);
}
}

}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}

finally
{
if (reader!=null)
reader.Close();
}
[/code]

the goal is to write the new string at the current line position
hence the get line position thinger. But i havent found an equivalent xmlwriter method.
Thoughts, laughter at the noob, its all appreciated

thanks

Comments

  • GogiGogi Posts: 258Member
    Hi,in my opinion,you should use XmlDocument class.It's the easiest way to work with XML files from C#.You can easily update your file with it.
    Just load your document,find the required node,edit it,and call save method.

    : hi im trying to update a line in an xml file. i have the following so far
    :
    : [code]
    : static void Main(string[] args)
    : {
    : string Location = args[0];
    : string OldVersion = args[1];
    : string NewVersion = args[2];
    : string testymctest;
    : int Lineposition;
    :
    : XmlTextReader reader = null;
    : XmlTextWriter writer = null;
    :
    : try
    : {
    : reader = new XmlTextReader( Location);
    : writer = new XmlTextWriter( Location, System.Text.Encoding.UTF8);
    :
    : while ( reader.Read())
    : {
    : testymctest = reader.Value;
    : if (testymctest == OldVersion)
    : {
    : //Lineposition = reader.LinePosition;
    : //writer.WriteString("
    : Console.WriteLine (testymctest);
    : }
    : }
    :
    : }
    : catch (Exception e)
    : {
    : Console.WriteLine ("Exception: {0}", e.ToString());
    : }
    :
    : finally
    : {
    : if (reader!=null)
    : reader.Close();
    : }
    : [/code]
    :
    : the goal is to write the new string at the current line position
    : hence the get line position thinger. But i havent found an equivalent xmlwriter method.
    : Thoughts, laughter at the noob, its all appreciated
    :
    : thanks
    :

Sign In or Register to comment.