A class to stop XML parser from converting UTF-8 entity hex characters

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












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)

this.input_string=input;
this.option=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;

default:
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:testAPril 2018testing.xml");
var names=myfile.Descendants("name").ToList();
foreach (var name in names)

XDocument infofile=tDocument.tParse(@"D:testAPril 2018info.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:testAPril 2018testing.xml",myfile);
Console.ReadLine();


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?







share|improve this question





















  • 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.
    – Michael Kay
    Apr 13 at 13:22










  • Are you really using the magic 0 and 1 as options? Why not an enum or some constants?
    – t3chb0t
    Apr 13 at 18:30











  • @t3chb0t will using an enum really make it more efficient or will it just make the code a bit more readable?
    – Don_B
    Apr 14 at 0:22










  • Don't you want your code to be readable?
    – t3chb0t
    Apr 14 at 5:13











  • @t3chb0t I just want to make the code more efficient, if possible..thats all
    – Don_B
    Apr 14 at 6:20
















up vote
1
down vote

favorite












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)

this.input_string=input;
this.option=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;

default:
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:testAPril 2018testing.xml");
var names=myfile.Descendants("name").ToList();
foreach (var name in names)

XDocument infofile=tDocument.tParse(@"D:testAPril 2018info.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:testAPril 2018testing.xml",myfile);
Console.ReadLine();


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?







share|improve this question





















  • 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.
    – Michael Kay
    Apr 13 at 13:22










  • Are you really using the magic 0 and 1 as options? Why not an enum or some constants?
    – t3chb0t
    Apr 13 at 18:30











  • @t3chb0t will using an enum really make it more efficient or will it just make the code a bit more readable?
    – Don_B
    Apr 14 at 0:22










  • Don't you want your code to be readable?
    – t3chb0t
    Apr 14 at 5:13











  • @t3chb0t I just want to make the code more efficient, if possible..thats all
    – Don_B
    Apr 14 at 6:20












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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)

this.input_string=input;
this.option=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;

default:
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:testAPril 2018testing.xml");
var names=myfile.Descendants("name").ToList();
foreach (var name in names)

XDocument infofile=tDocument.tParse(@"D:testAPril 2018info.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:testAPril 2018testing.xml",myfile);
Console.ReadLine();


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?







share|improve this question













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)

this.input_string=input;
this.option=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;

default:
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:testAPril 2018testing.xml");
var names=myfile.Descendants("name").ToList();
foreach (var name in names)

XDocument infofile=tDocument.tParse(@"D:testAPril 2018info.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:testAPril 2018testing.xml",myfile);
Console.ReadLine();


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?









share|improve this question












share|improve this question




share|improve this question








edited Apr 13 at 14:06









200_success

123k14142399




123k14142399









asked Apr 13 at 10:20









Don_B

1255




1255











  • 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.
    – Michael Kay
    Apr 13 at 13:22










  • Are you really using the magic 0 and 1 as options? Why not an enum or some constants?
    – t3chb0t
    Apr 13 at 18:30











  • @t3chb0t will using an enum really make it more efficient or will it just make the code a bit more readable?
    – Don_B
    Apr 14 at 0:22










  • Don't you want your code to be readable?
    – t3chb0t
    Apr 14 at 5:13











  • @t3chb0t I just want to make the code more efficient, if possible..thats all
    – Don_B
    Apr 14 at 6:20
















  • 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.
    – Michael Kay
    Apr 13 at 13:22










  • Are you really using the magic 0 and 1 as options? Why not an enum or some constants?
    – t3chb0t
    Apr 13 at 18:30











  • @t3chb0t will using an enum really make it more efficient or will it just make the code a bit more readable?
    – Don_B
    Apr 14 at 0:22










  • Don't you want your code to be readable?
    – t3chb0t
    Apr 14 at 5:13











  • @t3chb0t I just want to make the code more efficient, if possible..thats all
    – Don_B
    Apr 14 at 6:20















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.
– Michael Kay
Apr 13 at 13:22




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.
– Michael Kay
Apr 13 at 13:22












Are you really using the magic 0 and 1 as options? Why not an enum or some constants?
– t3chb0t
Apr 13 at 18:30





Are you really using the magic 0 and 1 as options? Why not an enum or some constants?
– t3chb0t
Apr 13 at 18:30













@t3chb0t will using an enum really make it more efficient or will it just make the code a bit more readable?
– Don_B
Apr 14 at 0:22




@t3chb0t will using an enum really make it more efficient or will it just make the code a bit more readable?
– Don_B
Apr 14 at 0:22












Don't you want your code to be readable?
– t3chb0t
Apr 14 at 5:13





Don't you want your code to be readable?
– t3chb0t
Apr 14 at 5:13













@t3chb0t I just want to make the code more efficient, if possible..thats all
– Don_B
Apr 14 at 6:20




@t3chb0t I just want to make the code more efficient, if possible..thats all
– Don_B
Apr 14 at 6:20















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191954%2fa-class-to-stop-xml-parser-from-converting-utf-8-entity-hex-characters%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191954%2fa-class-to-stop-xml-parser-from-converting-utf-8-entity-hex-characters%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation