Is it possible to perform a transform on multiple input XML files?
It doesn't appear to be possible using XslCompiledTransform, but is there an alternative way of applying an XSLT?
You can use the XSL function document() in your XSLT to reference an external XML file.
Apply the transformation to each input XML file individually and compose the resulting XML documents into a single document.
Compose the input XML files into a single document and apply the transformation, e.g.
XElement root = new XElement("root",
XElement.Load("file1.xml"),
XElement.Load("file2.xml"),
XElement.Load("file3.xml"));
XslCompiledTransform transform;
transform.Transform(root.CreateReader(), output);
With XSL function some security settings are necessary in C#. I believe this is the correct solution:
<xsl:include href="Filename"/>
This method handles multiple files.
Related
My strategy is:
Use XDocument.load() to read the XML files
Create an array with the root nodes of interest
if the root nodes (for one XML-file) does not exist in the other XML-file, create a new XML file and paste the node (incl. elements and attributes) in there.
Save the new document.
Are there any easier strategies than mine?
Do you have any advices of how I can solve this?
I want to convert my XML into a CSV file
"System/Main Chassis/Front Panel; "OMA/LCDprops/LCDobj/lcdaccessstate"; 0->disabled;1->enabled;2->N.A"
Where
"System/Main Chassis/Front Panel=path of the xml display page in the report"
"OMA/LCDprops/LCDobj/lcdaccessstate=node path(xpath)"
"0->disabled;1->enabled;2->N.A=all the possible values and their meanings for that particular node"
Can someone please let me know the best way to achieve this?
A more simple solution could be be an xsl stylesheet transormation from the xml to text file. You can manage the xsl transformation in C# with XslCompiledTransform object if you want
My requirement is to update an XML file (some elements identified via a parameter, with new attribute values again identified via a paramenter).
I am using XSLT to do the same via C# code.
My code is as below:
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(f_Xslt);
XmlReader xr = XmlReader.Create("SourceXML.xml");
XmlWriter xw = XmlWriter.Create("DestinationXML.xml");
XsltArgumentList argsList = new XsltArgumentList();
argsList.AddParam("", "", "");
...
...
...
xslt.Transform(xr, argsList, xw);
In my XSLT file, I first copy all elements, attributes. And then based on <xsl:template match = ... />, I update the elements, attr/values.
All this is saved to Destination.xml
What if I want all of this to happen on Source.xml itself.
Of course, the easiest solution(or my solution so far) is to replace the Source.XML with Destination.XML after I complete the XSLT.Transform successfully.
I think your transform-to-file-then-replace solution is as good as you're going to get. You don't want to overwrite the Source.XML file while reading it, even if .NET and the OS would let you.
In order to suggest a better alternative to transform-to-file-then-replace (TTFTR), I would ask, what is it about TTFTR that you feel is suboptimal?
The only alternative I can think of off-hand is to write the result of your transform to memory; and when the transform is finished, save the result from memory onto your source file. To transform to memory, pass a MemoryStream object as the argument to XmlWriter.Create().
You never should try to update in-place with XSLT. This is bad design and not in the spirit of a functional language.
This said, you can copy the source XML file in a temporary directory, then apply the transformation with an XmlWriter instance that is created to overwrite the original file.
As I said before, I wouldn't recommend this!
I have a string containing fully formatted XML data, created using a Perl script.
I now want to convert this string into an actual XML file in C#. Is there anyway to do this?
Thanks,
You can load a string into an in-memory representation, for example, using the LINQ to SQL XDocument type. Loading string can be done using Parse method and saving the document to a file is done using the Save method:
open System.Xml.Linq;
XDocument doc = XDocument.Parse(xmlContent);
doc.Save(fileName);
The question is why would you do that, if you already have correctly formatted XML document?
A good reasons that I can think of are:
To verify that the content is really valid XML
To generate XML with nice indentation and line breaks
If that's not what you need, then you should just write the data to a file (as others suggest).
Could be as simple as
File.WriteAllText(#"C:\Test.xml", "your-xml-string");
or
File.WriteAllText(#"C:\Test.xml", "your-xml-string", Encoding.UTF8);
XmlDocument doc = new XmlDocument();
doc.Load(... your string ...);
doc.Save(... your destination path...);
see also
http://msdn.microsoft.com/fr-fr/library/d5awd922%28v=VS.80%29.aspx
I have two inputs. I get as input one XML file. I have to create an XSD file for this XML file. This XML file has tags which depend on another input. But that XML file should have certain tags for sure. For example, the XML file has the following structure :
<A>
<B>
<C>...</C>
<D>...</D>
<E>
<F>...</F>
<G>...</G>
</E>
</B>
</A>
Here, in this XML file, A,B and E tags should be compulsory. But the tags C and D inside the B tag and tags F and G inside the E tag depends on another input. So I should create an XSD dynamically(i know that A,B and E tags should be present and I do know about the other tags from the other input) and validate the input XML file against the XML Schema. Can someone tell me how I can do this in C#?
I have no idea what you're asking.
An XSD is a blue-print for constructing a business-valid XML document. You do not generally create XSD documents dynamically. You obtain an XSD document so that you can create an XML document that will be valid in a specific business usage or validate XML documents against that schema.
I'm know XML Serialization in C# is covered in great depth on the web.
Have you looked at XSLT yet? It's very useful for creating one XML file based on another. If you can access an XSLT engine from C# (I guess that's possible), I can help you set up the XSLT stylesheet.