I'm trying to stop the xml parser from converting the UTF-8 enitity hex codes to its character alternative and also not adding the extra space in self-closing nodes after parsing.

I've created a class for this (say DB_class) shown as below

using System.IO;
using System.Xml.Linq;
class tDocument:XDocument
    string input_string;
    int option;

    public tDocument(string input,int opt)
    public static XDocument tParse(string path)
        string file_content = escape_string(File.ReadAllText(path), 0);
        XDocument doc = XDocument.Parse(file_content, LoadOptions.PreserveWhitespace);
        return doc;
    public static void tSave(string path, XDocument doc)
        doc.Save(path, SaveOptions.DisableFormatting);
        File.WriteAllText(path, escape_string(doc.ToString(), 1));
    public static string escape_string(string input_string, int option)
        switch (option)
            case 0:
                return input_string.Replace("&", "&");
            case 1:
                var x = input_string.Replace(" />", "/>");
                var y = x.Replace("&", "&");
                return y;

                return null;


Here is a random example to show how I'm using this class to do stuff (basically the program gets the value of node ID by matching the name from info.xml and adding it to the respective names attribute id in the xml file that I want to modify)

XDocument myfile=tDocument.tParse(@"D:\test\APril 2018\testing.xml");
var names=myfile.Descendants("name").ToList();
foreach (var name in names)
    XDocument infofile=tDocument.tParse(@"D:\test\APril 2018\info.xml");
    var data=infofile.Descendants("student").Where(x=>x.Element("s-name").Value==name.Value).Select(y=>y.Element("ID").Value).First();
    name.Add(new XAttribute("id",data));
tDocument.tSave(@"D:\test\APril 2018\testing.xml",myfile);

Here is the testing.xml and the info.xml

What I want to know is how can I make my class (DB_class) more efficient, and also if there is a better way doing what the class is doing?

  • \$\begingroup\$ A better way of doing it is not to do it at all, and instead to change the recipient of this data to be able to accept any standard XML. If the recipient can't handle UTF-8, then try putting the document through a null XSLT transformation that generates output in US-ASCII encoding. \$\endgroup\$ – Michael Kay Apr 13 '18 at 13:22
  • \$\begingroup\$ Are you really using the magic 0 and 1 as options? Why not an enum or some constants? \$\endgroup\$ – t3chb0t Apr 13 '18 at 18:30
  • \$\begingroup\$ @t3chb0t will using an enum really make it more efficient or will it just make the code a bit more readable? \$\endgroup\$ – Don_B Apr 14 '18 at 0:22
  • 1
    \$\begingroup\$ Don't you want your code to be readable? \$\endgroup\$ – t3chb0t Apr 14 '18 at 5:13
  • \$\begingroup\$ @t3chb0t I just want to make the code more efficient, if possible..thats all \$\endgroup\$ – Don_B Apr 14 '18 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.