0

I've a XML file like this

<?xml version="1.0" encoding="utf-8"?>
<MY_COMPUTER>
    <HARDWARE uid="" update="" functions=""  />
    <SOFTWARE>
        <GAMES uid="" update="" functions=""  url="">
            <GAME1 Game1-Attribute1="" />
            <GAME2 Game2-Attribute1="" Game2-Attribute2="" Game2-Attribute3="" Game2-Attribute4="" />
            <GAME3 Game3-Attribute1="" Game3-Attribute2="" Game3-Attribute3=""/>
            <GAME4 Game4-Attribute1="" Game4-Attribute2=""/>
        </GAMES>
    </SOFTWARE>
</MY_COMPUTER>

I'm trying to add new software types into this xml file for example browsers, browser will be same as game, it will have browser1, browser2 and a few of browsers will have attributes. I've used this

string filePath = "test.xml";

            XElement root = XElement.Load(filePath, LoadOptions.PreserveWhitespace);
 root.Add(
                        new XElement("BROWSER",
                        new XAttribute("uid",""), new XAttribute("update", ""),
                            new XElement("BROWSER2"),
                            new XElement("BROWSER3"),
                            new XElement("BROWSER4"), 
                            )
                            );

root.Save(filePath, SaveOptions.DisableFormatting);

but with this code its appending this under SOFTWARE, I know I probably made a really big beginner mistake but I couldn't fix it, can someone help me? I've also checked a lot of questions about this on stackoverflow but I still couldn't manage it. People say there are many ways like using LINQ or stream I don't know which one to use but this file wont be really huge so I just need a way that will work Thanks

1 Answers1

1

The reason of this xml piece adding after software element is that you are adding it to a root element itself (root.Add).

If you want to add it inside the software element, you should modify your code accordingly.

Find required element and invoke its Add method instead.

var softwareElement = root.Descendants("SOFTWARE").First();

softwareElement.Add(
    new XElement("BROWSER",
        new XAttribute("uid", ""), new XAttribute("update", ""),
        new XElement("BROWSER2"),
        new XElement("BROWSER3"),
        new XElement("BROWSER4")
    )
);

Then save all the xml as previously.

root.Save(filePath, SaveOptions.DisableFormatting);
Uladzislaŭ
  • 1,680
  • 10
  • 13
  • Thank you for your assistance, It's working as I want now, can I ask you 1 more thing? I use xmlwriter to save the file at the first place but I dont know how to go underline after each tag so the file is so hard to read when I open and want to check, is there any command to do it? – ShinoLexTazy May 15 '18 at 07:38
  • @ShinoLexTazy Your output is not formatted nicely because of `SaveOptions.DisableFormatting` tweak. You could try to remove `LoadOptions.PreserveWhitespace` as well, to see proper tabulation and line feeds in the output. – Uladzislaŭ May 15 '18 at 07:46
  • my XMLcreation method is like this: XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.NewLineOnAttributes = true; using (XmlWriter xmlWriter = XmlWriter.Create("test.xml", xmlWriterSettings)) { //my stuff } But still when I create it the xml file looks like a mess have to open the file and put enter's to check if its right – ShinoLexTazy May 15 '18 at 07:55
  • after removing the LoadOptions and SaveOptions its appending correctly but when I dont use the append method and only use my creation method its still a mess, but it's okay since I'll append a lot of times to the file it will be okay if its ordering it after my append – ShinoLexTazy May 15 '18 at 08:01
  • @ShinoLexTazy Try to play with `XmlWriterSettings` object. Use a default one for start and make modifications one by one. Check [this](https://msdn.microsoft.com/ru-ru/library/system.xml.xmlwritersettings.indent(v=vs.110).aspx) one for example. – Uladzislaŭ May 15 '18 at 08:05