A class to stop XML parser from converting UTF-8 entity hex characters
Clash 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?
c# parsing xml escaping
add a comment |Â
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?
c# parsing xml escaping
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 magic0
and1
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
add a comment |Â
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?
c# parsing xml escaping
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?
c# parsing xml escaping
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 magic0
and1
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
add a comment |Â
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 magic0
and1
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
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
and1
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