XML to CSV using XMLreader - c#

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

Related

XmlReader - read xml with colons in tag names

I have very complex XML that I need to read with XmlReader.
The elements in the xml are as the following:
<log:event>
<ev:logger>some text</ev:logger>
<ev:line>24</ev:line>
<ev:ex>
<ev:levelone>some message</ev:levelone>
<ev:leveltwo>some other message</ev:leveltwo>
</ev:ex>
</log:event>
XmlReader will not know how to read this since it does not have name space definition on each xml tag.
I would have done that programmatically (appending namespace to the strings), but the file is huge and I its impossible.
(I dont control the file creation).
Any suggestion how can that file be read as xml without namespacing?
Thanks!
You can still append namespaces, just read the file into memory and manipulate it there. I do it with several XML-based API's from machine manufacturers that doesn't comply with XML standards to make it easier to read with normal xml parsers

Creating XSD Dynamically

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.

XSLT: transfer xml with the closed tags

I'm using XSLT transfer an XML to a different format XML. If there is empty data with the element, it will display as a self-closing, eg. <data />, but I want output it with the closing tag like this <data></data>.
If I change the output method from "xml" to "html" then I can get the <data></data>, but I will lose the <?xml version="1.0" encoding="UTF-8"?> on the top of the document. Is this the correct way of doing this?
Many thanks.
Daoming
If you want this because you think that self closing tags are ugly, then get over it.
If you want to pass the output to some non-conformant XML Parser that is under control, then use a better parser, or fix the one you are using.
If it is out of your control, and you must send it to an inadequate XML Parser, then do you really need the prolog? If not, then html output method is fine.
If you do need the XML prolog, then you could use the html output method, and prepend the prolog after transformation, but before sending it to the deficient parser.
Alternatively, you could output it as XML with self-closing tags, and preprocess before sending it to your deficient parser with some kind of custom serialisation, using the DOM. If it can't handle self-closing tags, then I'm sure that isn't the only way in which it fails to parse XML. You might need to do something about namespaces, for example.
You could try adding an empty text node to any empty elements that you are outputting. That might do the trick.
Self-closed and explicitly closed elements are exactly the same thing in any regard whatsoever.
Only if somewhere along your processing chain there is a tool that is not XML aware (code that does XML processing with regex, for example), it might make a difference. At which point you should think about changing that part of the processing, instead of the XML generation/serialization part.

How to extract XML from the WebBrowser control?

I want the same as WebBrowser.Document.Body.InnerHtml, but as an XML representation.
Are you using WebBrowser to browse an XML document and want to get to that XML in code, or are you trying to browse to an HTML page and represent HTML as XML?
If the former you can likely just get the raw text from the WebBrowser (maybe InnerText instead of InnerHTML) and parse it as XML.
If the latter, the problem is, HTML isn't XML (unless it's XHTML).
You can convert it to XML with 'tidy' tools but the representation accuracy depends on how well formed the orginal HTML is.
TidyCOM will clean up HTML to XHTML.
Here's how to use it from C#.
IE's document has an expando property named "XMLDocument". You can access it via its IDispatchEx interface.
You can get the document's COM interface via Document.DomDocument.

XSLT transform with multiple XML input files

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.

Categories

Resources