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
Related
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 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.
I've been working on a project (C#) and part of it was filling a data grid with an embedded xml file.
Although I've now found a way to make this work, i am still confused as to to theory behind it. And I'd like to stop and make sure i fully understand it before i continue with this project.
The code that i have working currently is;
XmlDataDocument myXML = new XmlDataDocument();
StringReader mytempXML = (new StringReader(BasicTest.Properties.Resources.myxml));
myXML.DataSet.ReadXml(mytempXML);
What is confusing to me is that before this solution, I was trying the below;
myXML.DataSet.ReadXml(BasicTest.Properties.Resources.myxml);
and it wasn't working. However using the full file path (like below) was working.
myXML.DataSet.ReadXml("C:/..etc../myxml.xml");
The Question I have is: why is a StringReader required for the ReadXml method if you're reading from a resource, but using a full file path works without?
If anyone could provide an explanation, that would be great.
Thanks.
This is because the ReadXml method takes a string. That string must be the name of a file. It cannot be XML. If you pass it a string that is XML, it will think that is the name of the file! It doesn't have the smarts to look at the string and ask "Is this string XML, or is it a file name?" and figure that out.
// Summary:
// Reads XML schema and data into the System.Data.DataSet using the specified
// file.
//
// Parameters:
// fileName:
// The filename (including the path) from which to read.
public XmlReadMode ReadXml(string fileName);
By wrapping the XML in a stringreader or a stream or something, you are calling a different overload, that expects XML instead of a file name.
I have an XDocument file that I have loaded. (confirmed the working). I need to validate this document. To do so I have an XSD file that I attached to the project as a embedded resource. I load
the xsd with these line of code:
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("Project.Models.Ci.def.xsd");
How can I now validate the XML against this XSD? I only need to know if the document is valid or not, so nothing fancy.
There is a Validate extension method that might meet your needs. Take a look at this documentation on MSDN - it has sample code as well:
Extensions.Validate Method (XDocument, XmlSchemaSet, ValidationEventHandler, Boolean)
I'm doing an xslt transform of the xml within an excel file using Saxon where the transform is being done on the .rels xml file within the xlsx file. I currently have a workaround in place where I have unzipped the entire contents of the xlsx file into a seperate folder. However, for simplicity's sake my program takes the xlsx file as it's input, so I was wondering if there was a simpler way for my program to point to the xml within the xlsx file without the need to unzip the contents. I've tried pathing to file.xlsx\_rels\.rels but that doesn't seem to work. The code I'm current using for this input is
String inputFile = "file.xlsx_folder\\_rels\\.rels"
XdmNode input = processor.NewDocumentBuilder().Build(new Uri(inputFile));
but I would like to have that point directly within the xlsx.
Yes there is a simpler way, you don't need to unzip the whole file.
Instead of the file path you've passed as a parameter to the Saxon APIs Build function, pass an XmlReader or Stream instance of the uncompressed part of the xslx file.
The Open static method of System.IO.Packaging.Package can be used to get a Package instance on which you call GetStream to uncompress the part you need and return it as a Stream. Package handles files conforming to the Open Packaging Convention, like Excel's xslx format.
The code below uncompresses the package part to a Stream, uses this to create an XmlReader instance, and finally passes the XmlReader as the parameter to the Build function:
string filename = "c:\\test\\file.xslx";
string partPath = "/_rels/.rels";
Package xpsPackage = Package.Open(fileName, FileMode.Open)
Uri partUri = new Uri(partPath, UriKind.Relative);
PackagePart xpsPart = xpsPackage.GetPart(partUri);
Stream xpsStream = xpsPart.GetStream(FileMode.Open)
XmlReader xmlReader = XmlReader.Create(xpsStream);
XdmNode input = processor.NewDocumentBuilder().Build(xmlReader);