How to change xml file in c #?

<Customers>
  <Customer1>
    <Name>Bobby</Name>
    <Age>21</Age>
    <Address>Panjim</Address>
  </Customer1>
  <Customer2>
    <Name>Peter</Name>
    <Age>32</Age>
    <Address>Panjim</Address>
  </Customer2>
  <Customer4>
    <Name>Joel</Name>
    <Age>32</Age>
    <Address>Mapusa</Address>
  </Customer4>
</Customers>

So, I want to delete a specific item, and when I delete the first item ie customer1, I want to update other items. I want to say that I want to do customer3, customer2 and customer2, customer1. Can anyone help me achieve this?

+3
source share
4 answers

I would say that your problem is not how you could rename your nodes with minimal effort, but into the structure of your XML file.

You said that the order of the clients is not important, and obviously the client tag number is also not important, since you want to rename the tags when deleting.

So perhaps this structure just creates unnecessary complexity and extra work for you.

, , , - node, . , - ? , (, Guid) , .

.

<customers>
    <customer>
        <guid>07fb-877c-...</guid>
        <name>Notch</name>
        <age>34</age>
        <address>street</address>
    </customer>
    <customer>
        <guid>1435-435a-...</guid>
        <name>Sam</name>
        <age>23</age>
        <address>other</address>
    </customer>
<customers>
0

:

class Program {
    static void Main(string[ ] args) {
        XDocument doc = XDocument.Load("D:\\file.xml"); //example file
        doc.Root.SwitchAndRemove("Customer1");
        doc.Save("D:\\file.xml");
    }
}

public static class Utilities {
    public static void SwitchAndRemove(this XElement customers, XName name) {
        var x = customers.Descendants().Where(e => e.Name == name).Select((element, index) => new { element, index }).Single();

        int count = 0;
        XElement temp = x.element;
        foreach (XElement el in customers.Nodes()) {
            if (count == x.index + 1) {
                temp.RemoveAll();
                temp.Add(el.Descendants().ToArray());
                temp = el;
            }
            else
                count++;
        }

        temp.Remove();
    }
}

xml, :

<?xml version="1.0" encoding="utf-8"?>
<Customers>
  <Customer1>
    <Name>Peter</Name>
    <Age>32</Age>
    <Address>Panjim</Address>
  </Customer1>
  <Customer2>
    <Name>Joel</Name>
    <Age>32</Age>
    <Address>Mapusa</Address>
  </Customer2>
</Customers>
+3

, Customer1, , XML , XML-, #, XDocument XmlReader, xml, , "Temp.xml", Customer1 . .

XML , , "xmlstring". "", , "Customer2" "Customer1", "Customer3" "Customer2" ..

And now delete the source XML file and write the line "xmlstring", using the stream creator, to the file name "YourFileName.xml"

Here it is. Hope this solution works for you. Try this, and if you can’t do it, share the code that you tried and we will offer you how to solve it.

0
source

taken from your comment that the order does not need to be saved, you can do it

public static void RemoveCustomer(XElement customers, XElement removeThis){

   var last = customeers.Elements().Last();
   if(last != removeThis){
       foreach(var element in removeThis.Elements()){
            element.Value = last.Element(element.Name).Value;
       }
   }
   last.Remove();
}

It effectively replaces the one that needs to be deleted with the latter (if the latter should not be deleted) and thereby eliminates the need to rename any of the other elements

0
source

All Articles