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.
Related
I have an xml file which has a CDATA section, which has again xml data. I would need to get a specific node from the xml within CDATA and create one more node of the same type and save the xml.
The replace and save functionality works for 1 input. but i want the tag to be appended in the same file. I hope i am clear!
Have o look at this thread XML parsing : Reading CDATA You probably need to read the CDATA value, convert, create node and write it back
I need to validate a flat file ( text file ) with an XSD file (schema). I found to do this for a XML file but not for text file.
Is there any base class to do that ?
The contents of the text file is as follows:
Header
SubHeader (many)
Records (many)
Footer
An XSD cannot be used to validate an arbitrary text file, only an XML file.
The validation rules specified in the W3C XML Schema Recommendation are defined against XML elements and attributes, not arbitrary text:
Throughout this specification, Definition: the word valid and its
derivatives are used to refer to [the following:]
[...] whether an element or
attribute information item satisfies the constraints embodied in the
relevant components of an XML Schema
[Order rearranged and emphasis added from original source.]
XSD stands for Xml Schema Definition. You can only use it to check xml, not arbitrary text.
Your best bet would be to refresh your Regex skills.
May be you want to use Flat File Checker, is an open source application for data validation in import and export files, then This can help you https://www.codeproject.com/Articles/43398/Validating-data-with-Flat-File-Checker
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
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
I am Currently Facing A problem. I am loading a xml file in C# and remove some nodes from it and appending some nodes. now problem is that when i am doing removal from the xml file then there are some empty lines created automatically ,so i want to remove these line .
And when i append some nodes to the parent node in xml then i want the new line in each ending tag
For Eg. My Xml file is
<intro id="S0001">
<title>Introduction Title</title>
<para>This is a paragraph. Note that paragraphs can contain other block–level objects, such as lists, as well as directly containing text.</para>
<para>The introduction can contain all of the text objects that a section can contain, except that it cannot be divided into parts, sections and sub–sections.</para>
<para>The introduction can contain tables:</para>
</intro><part>
<no>Part A</no> Article Structure <sup>(Part Title)</sup><section1 id="S0002">`enter code here`
<no>Sect 1</no>
<title>First Section in Part 1 <sup>(Section 1 Title)</sup></title>
<shortsectionhead>Short Section Header</shortsectionhead>
<para>This is a section in the first part of the article.</para>
</section1><section1 id="S0003">
Code:
XmlNode partNnode = xmlDoc.SelectSingleNode("//part");
XmlNode introNode=xmlDoc.SelectSingleNode("//intro");
XmlDocumentFragment newNode=xmlDoc.CreateDocumentFragment();
newNode.InnerXml=partNnode.OuterXml;
introNode.ParentNode.InsertAfter(newNode,introNode);
partNnode.ParentNode.RemoveChild(partNnode);
partNnode = xmlDoc.SelectSingleNode("//part");
nodeList = xmlDoc.SelectNodes("//section1");
foreach (XmlNode refrangeNode in nodeList)
{
newNode=xmlDoc.CreateDocumentFragment();
newNode.InnerXml=refrangeNode.??OuterXml;
partNnode.AppendChild(newNode);
}
Please help me
Thanks in advance
If you load and save a XMl file with C#, then the XML should be formatted correctly (an easy way to format strange looking XML files is just to load and save them with some C# code).
If I understand your question correctly, then you are just not happy with the format of the XML file?
Like you want (A):
</intro><part>
But you get (B):
</intro>
<part>
If that is the question, then, in my eyes, you just want a strange thing. Because...
a) Code doesn't care how the XML file is formatted and
b) The format in (B) is the correct one
If you, for what reason ever, want to change it, then you have to parse through the XML file, opening it as a string and checking manually for closed and opened tags.