Is it possible to replace symbols in XML file and read that XML file in C#?
I have xml file which contains symbol like (←,↑) and I want to print that xml file on browser, but I had getting errors.
So is it possible to replace that symbols in xml and display valid xml file on browser?
You can use IsXmlChar to find out the invalid XML characters and remove them.
Try out this
string sMyXMLContent = "<your xml string>";
var validXmlChars = sMyXMLContent.Where(ch => XmlConvert.IsXmlChar(ch)).ToArray();
This will remove the invalid xml characters.
Related
I'm trying to load a remote XML file using C# but there are unescaped characters (e.g. '\', '&') that are preventing the file from being loaded by my program since I keep getting the error
"An error occurred while parsing EntityName"
Ideally I want to load the XML file and replace these characters during runtime so that the file is read properly.
String URLString = "REMOTE XML FILE PATH";
XmlTextReader reader = new XmlTextReader(URLString);
XmlDocument xmlDoc = new XmlDocument();
// This is where the error is occurring
xmlDoc.Load(URLString);
Any help would be greatly appreciated!
Before I get into the issue, I'm aware there is another question that sounds exactly the same as mine. However, I've tried that solution (using Notepad++ to encode the xml file as UTF-8 (without BOM) ) and it doesn't work.
XmlDocument namesDoc = new XmlDocument();
XmlDocument factionsDoc = new XmlDocument();
namesDoc.LoadXml(Application.persistentDataPath + "/names.xml");
factionsDoc.LoadXml(Application.persistentDataPath + "/factions.xml");
Above is the code I have problems with. I'm not sure what the problem is.
<?xml version="1.0" encoding="UTF-8"?>
<factions>
<major id="0">
...
Above is a section of the XML file (the start of it - names.xml is also the same except it has no 'id' attribute). The file(s) are both encoded in UTF-8 - in the latest notepad++ version, there is no option of "encode in UTF-8 without BOM" afaik UTF-8 is the same as UTF-8 without BOM.
Does anyone have any idea what the cause may be? Or am I doing something wrong/forgetting something? :/
You are receiving an error because the .LoadXml() method expects a string argument that contains the XML data, not the location of an XML file. If you want to load an XML file then you need to use the .Load() method, not the .LoadXml() method.
I have an XML file which is read and copied to a new XML file at runtime.
Sometimes, the XML contains invalid XML , such as -
</SST_DSS.TickerConfig>erConfig>
Is there any way to 'clean' the xml before writing to the new XML file?
Currently I am writing to the new file like so...
string workingPath = System.IO.Path.Combine(#"C:\SST Software\DSC\Tickers\", currentTicker + ".tck");
string configString = System.IO.File.ReadAllText(workingPath);
File.WriteAllText(newFilePath, configString);
How can I remove the 'erConfig' before writing to new XML file?
I am trying to read the XML Documentation file (C#) using this ocde -
Type classType = typeof(Point);
string documentationFileLocation = classType.Assembly.CodeBase;
if ( !string.IsNullOrEmpty(documentationFileLocation) && documentationFileLocation.StartsWith("file:///") )
{
documentationFileLocation = documentationFileLocation.Replace(".exe",".xml");
documentationFileLocation = documentationFileLocation.Replace("file:///","");
if(File.Exists(documentationFileLocation))
{
XElement document = XElement.Load(documentationFileLocation);
// Some Code Logic Here using LINQ
}
else
{
Console.WriteLine("Please Go to Project Properties->Build and check 'XML Documentation file'");
I have a LINQ Query after XElement document = XElement.Load(sr) which dosen`t work,
So I put a breakpoint in the LINQ Query and I am getting this error -
XmlException - Data at the root level is invalid. Line 1, position 1.
How I can fix it?
Edit:Changed the code a little - just deleted StreamReader
Well, it sounds like it simply isn't a valid XML file.
If you print out the result of sr.ReadToEnd() instead of calling XElement.Load, what does it look like? If you try to load the file into an XML editor, what happens?
Btw, it's better to use a using statement than calling Dispose explicitly: with your current code, the StreamReader isn't disposed if Load throws an exception.
Finally, is there any reason you're not just using XElement.Load(documentationFileLocation)?
Have you tried XDocument.Load() instead of using XElement? If the file begins with an XML declaration <?xml ..., you might get this error when trying to load an element from it.
Edit: the file you pasted on pastebin has no encoding specified. Can you try to open this file in notepad and re-save it as ANSI, the see if it loads? Just to make sure that we don't have an encoding or BOM problem.
how can i load the contents of an xml file at a url into a string?
eg there is an xml file at http://www.example.com/test.xml
I want the text of the xml to be assigned to a string.
How can i do that using c#?
thanks
Well, it seems to me that the fact it's XML is irrelevant - just download it as a string:
WebClient client = new WebClient();
string text = client.DownloadString(url);
Other way to get the xml file is through HttpRequest, then you can parse the XML, if that's what you need:
previous question on the matter (with code sample)