Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.