Removing unwanted XML at runtime - c#

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?

Related

Validating Xml File using xsd string in XmlSerilizer method

I have a xml file in cmd argument and xsd file in resources. var xsdstring = Recourses.EmployeeSchema; How can i use this xsdstring to validate xml file using xmlDeserlize method. I read a lot of article but can't find solution

How to read XML file which contains different symbols e.g. (←)

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.

How to Transform from string containing xml and xslt to Excel in c#?

i am exporting excel by transform xml,xslt into xls.
Below is my coding :
ds.WriteXml(MyXmlPath);
XPathDocument xmlDoc = new XPathDocument(MyXmlPath);
XslCompiledTransform XSLTransform = new XslCompiledTransform();
XSLTransform.Load(AppBasePath + #"\Master\XSLT\" + strSelectedXSLT.ToString() + ".xslt");
XSLTransform.Transform(MyXmlPath, MyExcelPath);
From the above coding i am write xml into disk for the given path by using dataset. And read from the disk path in order to transform xls file.
****PROBLEM : Instead of writing & Reading the xml content , why should i write the xml content into string and convert **BECAUSE ITS TAKE HEAVY TIME TO WRITE EXCEL. so i tried below coding . But its not working . ******
StringWriter sw = new StringWriter();
ds.WriteXml(sw, XmlWriteMode.IgnoreSchema);
string xmlcontent = sw.ToString();
XslCompiledTransform XSLTransform = new XslCompiledTransform();
XSLTransform.Load(AppBasePath + #"\Master\XSLT\" + strSelectedXSLT.ToString() + ".xslt");
XSLTransform.Transform(xmlcontent, MyExcelPath);
Any suggestions ?
When using Transform(string,string) both of the parameters should be URI. In your first code example you're using a file URI for the BOTH parameters of Transform(). This is correct. For the second example, you have tried to pass in the xml content not a URI as a string for the first parameter. I dont know why you didn't see an exception at this point.
If you want to pass xml content directly into Transform() you will need to use one of other method signatures that accepts an XmlReader or IXPathNavigable for the xml input. However, when using one of these other Transform() signatures, you'll also need to Stream the output to a file or use XmlWriter
Check out this: How to transform XML as a string w/o using files in .NET?

How to use string instead of file for XSLT.Load and XSLT.Transform method?

I am using http://www.thescarms.com/dotnet/XSLT.aspx to Convert comma delimited data (CSV) to XML using XSLT template.
It uses the foll. 2 lines of .NET code:
XSLT.Load(mstrInputXSLTFile, resolver);
XSLT.Transform(mstrInputCSVFile, mstrOutputXMLFile, resolver);
I am looking for a way in which I can use the string contents (contents of the XSLT, CSV file) instead of files in above 2 methods.. Any help will be usefull.
I am planning to implement this logic in a WCF webservice which will receive the csv string. If there is no workaround then I will have to create temp files based on the values of csv and xsl received. Process the conversion of csv to xml on the server and return the xml output to the client. Then delete the files created above.
If you want to load the input from a string then create an XmlReader over a StringReader over your string e.g.
XslCompiledTransform proc = new XslCompiledTransform();
using (StringReader sr = new StringReader(stringVar))
{
using (XmlReader xr = XmlReader.Create(sr))
{
proc.Load(xr);
}
}
There is no suitable method in XslTransform which does what you want.
However, you could write your own extensions methods (I would call them Parse..), which take content as string, create files in the temporary directory, and load/transform them by the suitable methods.

how can i load the contents of an xml file at a url into a string?

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)

Categories

Resources