Editing / Updating XML Using C # - Programming - HWzone Forums
adplus-dvertising
Skip to content
  • Create an account
  • About Us

    Hello Guest!

     
    Please note - in order to participate in our community, comment and open new discussions, you must join as a registered member.

    Our members enjoy many advantages, including the ability to participate in discussions, enjoy raffles and promotions for members of the site, and receive our weekly content directly by email.

    Do not like being harassed by email? You can register for the site but do not submit your registration to the weekly email updates.

Edit / update XML with C #


Dimka

Recommended Posts

Hello everyone,

I'm not so familiar with C # nor XML so forgive me for the terms.

I'm trying to write a program that finds a node and updates an element value.

I searched for examples on the net and found at the end something built-in of C #, it works but has a side effect, it additionally adds the XML version at the top of the file.

How do I prevent this? I just want to update the entry and that's it, no other modification to the file is acceptable ...

Relevant code snippets:

// Load xml & Find root element
XDocument xdoc = XDocument.Load (myfile.xml);
var element = xdoc.Element ("Section"). Element ("Element1");

// update
element.Element ("Path"). Value = "newPath";

// Save
xdoc.Save (myfile.xml);

The line he adds (which I want to avoid) is something like this:

Link to content
Share on other sites

The link does not help my problem.

xmlwriter creates new elements and completely overwrites the file ... Unable to open for editing only.

The question is how do I open the file, change the value my string and save the file ... Can I have a code snippet ready for it? (I've been searching a lot and can't find anything helpful).

My file is structured more or less like this:



my string

Of course without this addition:

Link to content
Share on other sites

There is no such thing as "editing" an xml file. You can open the file, load its contents into memory, change the contents in memory and then write all the contents back to the file (the only way to "edit" it is by swapping - and for that you need what you write to be the same length as what you delete).

What is your code? You probably have a mistake somewhere ...

Link to content
Share on other sites

I'm not mistaken (I just checked xmlwriter behavior on a ready-made file), I do not know what to write to read the file to memory with xmlwriter ...

From what I found on the net, one should somehow use xmlreader and then paste (if I understood correctly) the node using xmlwriter.

This is what works in the meantime (only it adds the line of declaration):

// Load xml & Find root element
XDocument xdoc = XDocument.Load (file);
var element = xdoc.Element ("object"). Element ("item");
element.Element ("string"). Value = env;

// save
xdoc.Save (file);

Link to content
Share on other sites

As for the code, it was really junk, I tried to see how the writer behaves and I thought that if I try to create a node of the same name, then it will overwrite the existing one, it turns out he created a new file for me with only one node ... after that I found out it really is not Enough because I was missing the xmlReader interest that I did not even know how to operate (I am really not experienced in working with programming files in general).

I solved the problem indirectly (though not elegantly).

I read the file into an array of rows (split), and saved the array starting from the second row ...

Link to content
Share on other sites

ארכיון

This discussion has been archived and new comments can not be added.

  • Latest news

  • Buzz-Zone: Everything hot on the net

  • Popular now

×
  • Create new ...

At the top of the news:

new on the site