Converting XDR to XSD - c#

I have a an xml schema document in this format
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
<AttributeType name="scale" default="4.0"/>
<ElementType name="GPA" content="textOnly" dt:type="float">
<attribute type="scale"/>
</ElementType>
<AttributeType name="studentID"/>
<ElementType name="student" content="eltOnly" model="open" order="many">
<attribute type="studentID"/>
<element type="GPA"/>
</ElementType>
</Schema>
I wanted to generate classes in C# from given schema. I checked online that XSD.exe
can generate classes only from W3C XML Schemas.
Is dere ne way to convert this format into W3C?
I am new to XSD and tried rewriting it but gettin loads of errors.
Please help.
Thanks in Advance

You could try this:
C# Auto generation of class objects from XSD
Alternatively, I just wrote a generic modeler that's open source. You can use it to generate classes in whatever language you'd like.
https://github.com/homer6/modeler
If you fork the crudecppmodeler branch, it'd be similar to C#. Either that, or I can specifically design it for you.
It's not based off of XSD, but I could adapt it to. I plan to write support for multiplicity in the next few days.
Here's the sample format: https://github.com/homer6/modeler/blob/crudecppmodeler/simple.jm
Hope that helps...

Related

Is having a scalar type and element inside a parent element bad design in XML?

We currently have the following structure as part of an API endpoint response.
<elements>
<element arg1="" arg2="">id</element>
</element>
Now, I would like to add a sub-element to the element without changing the existing structure. This is what I had when adding the sub-element.
<elements>
<element arg1="" arg2="">id
<subelement arg1="" arg2="" />
</element>
</element>
It seems like the easiest solution and as far as I read it's valid XML.
But is it bad design? Would the API users be able to handle this well in their languages (mostly C#)?
Your candidate design uses mixed-content for data-oriented XML. You're right to suspect that it's non-ideal. Users will not be able to leverage the benefits of markup to access id separately from subelement; parsing and XPath access would be harder than necessary. If you're determined to use XML for data-oriented content, it would be better to have id wrapped in its own tag (or to use attributes for scalars -- see XML Element vs XML Attribute).
More broadly, realize that using XML for data-oriented content has been out-of-favor for good reasons for more than a decade. New data-oriented APIs should not be based on XML. JSON is a commonly preferred alternative.
XML in general and mixed-content in particular are best used for document-oriented data.
See also
Can an XML element have both text and element content?
Need example of mixed content in XSD
What is a markup language? (XML vs YAML vs JSON)
XML vs JSON to represent database data

Generate C# class from XSD (xhtml.blkstruct.class)

I have an xsd file, simplified as below with an element referencing the xhtml.BlkStruct.class. I have tried xsd.exe and xsd2code trying to generate a C# class from it, but I am always getting errors.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xs:complexType name="test">
<xs:group ref="xhtml.BlkStruct.class"/>
</xs:complexType>
</xs:schema>
This is the error (reference to an undeclared / not declared model group)
Error: Verweis auf eine nicht deklarierte Modellgruppe 'http://www.w3.org/1999/xhtml:xhtml.BlkStruct.class'.
Can anybody help me out what is going wrong here?
Background I am tryint to XmlSerialize a string containing (simple) HTML/XML mixed with sting content, so things like
&ltp&gt, &ltdiv&gt, etc
for example
Hello &ltu&gtunderlined text&lt/u&gt &ltb&gtbold text&lt/b&gt world
Well, you're not supplying the xhtml.BlkStruct.class anywhere in your XSD. You might need to include its definition to succeed in the code generation. Download the file from http://www.w3.org/MarkUp/SCHEMA/ and add a
<xs:include schemaLocation="xhtml-basic11-model-1.xsd">
But do you really need a strongly typed DOM? A simpler solution is to define your test field as string, and include your HTML as CDATA or encoded html. My experience is that mixing XML with HTML is always a bad design choice and you have better luck with treating the HTML part as text.

how to compare XML strings in C#?

In DB I have XML strings stored in a column. Below is my XML structure:
<?xml version="1.0"?>
-<ProductAttributes xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<Attribute Required="false" ID="2" Name="Color">
<Value ID="18">Light Pink</Value>
</Attribute>
-<Attribute Required="false" ID="1" Name="Size">
<Value ID="9">XL</Value>
</Attribute>
</ProductAttributes>
Another XML is:
<?xml version="1.0"?>
-<ProductAttributes xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<Attribute Required="false" ID="1" Name="Size">
<Value ID="1">S</Value>
</Attribute>
-<Attribute Required="false" ID="2" Name="Color">
<Value ID="4">Red</Value>
</Attribute>
-<Attribute Required="false" ID="3" Name="Weight">
<Value ID="6">10gr</Value>
</Attribute>
</ProductAttributes>
Notes
There can be n number of xml strings and each xml string can have m number of tags
Attribute nodes can in different order, for example in 1st attribute Id=1 can be first attribute and in 2nd attribute Id=1 can be last.
Requirement is not compare these n XML strings and find if any of strings has complete duplication of attributes (this comparison will consider values as order can be different).
Please guide and help me.
don't compare strings of XML. Use them as input to an XML parser that will turn them into XML trees, then search the trees for matching elements and compare their list of attributes.
You could iterate all nodes of xml doc A and for each node, look up its xpath in xml doc B. If any paths are not found or the path is found but the value is different, the doc's are not 'the same'.
You'd then have to do the same for all nodes in B, checking the xpaths in A, to ensure there's nothing "in B but not in A"
Optimise by quitting as 'not equal' as soon as an xpath is not found or the values don't match.
You may want to try The XML Diff and Patch GUI Tool which you can download from here. I've used it before and it works ok.

Creating an XSD schema

I have an xml tag:
<ROW field1="value 1" field2="value 2" ... />
fieldi has a string value, and number of attributes fieldi is variable, but not less than 1. Is it possible to create an xsd schema for this tag?
possible xml document
<ROWDATA>
<ROW field1="dfgdf" field2="ddfg"></ROW>
<ROW field1="dfedf" field2="djkfg" field3="cdffd"></ROW>
<ROW field1="dfedf" field2="djkfg" field3="cdffd" field4="dfedf" field5="djkfg" field6="cdffd"></ROW>
</ROWDATA>
in this xml document, which I receive from a web server, can be a variable number of attributes field (I noted them as fieldi, where i means the order of a specific attribute field)
So I have, unknown number of ROW elements and unknown number of field attributes in the ROW element
Thanks
If you're using Visual Studio 2008:
Open your Xml file in Visual Studio
Go to the 'Xml' menu option at the top of the screen
Choose 'Create Schema'
This will generate your xsd schema(s)
EDIT
Try this example for details on setting minOccurs (on elements) or required (on attributes) so you can manipulate your derived schema.
If you are not comfortable in writing XSD yourself, use some generator like this.
EDIT: Based on your XML in comments, I can think of below structure of XSD
<xsd:element name="FieldHeader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Fields" type="xsd:string"/> <!--use minOccurs maxOccurs here-->
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="fieldi">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:simpleType name="Fields">
<xsd:list itemType="fieldi" />
</xsd:simpleType>
I think I have understood your requirement. Just to avoid misconceptions, let me reveal what I have understood once:
"You have an xml file which contains an element with the name fieldi, comes with set of some unknown attributes. Which means you don't know [or say don't want] the names and values of those attributes. Just want to see, there is at-least 1 attribute appearing",
Well. sorry to say that, this requirement is running out of capability of XML-schema. :-[
You cannot have attributes undeclared in schema. If it appears in xml, it requires to have a proper definition for that. There is something called <anyAttribute/> [click-here] which again requires definition [somewhere, in another linked schema].
1) Defining all the possible attributes making use="optional", doesn't look practically possible. And also your last requirement go skipped.
2) If it is possible then, convert all the attributes to elements [using transformation, or you can either ask the sender to do so, I don't know how complicate it is in your case], And define element <any/>, which sounds somewhat comfortable. but your requirement [at-least one attribute must appear] is still not achieved.
so this is it I can add-up for you. If you can change the requirement or input xml structure then let me know, I will see, whether I can help you in any-other ways ..
regards,
infant-pro
I solved the problem but in other way, by controlling the deserialization of the xml document in the way I need. However, I don't like this solution, because I had wanted to create classes from the xsd scheme and use them in my code.
Anyway, thanks to everybody

How to get a collection of objects representing the diff between two XML texts?

What is the best way in C# to get a collection of objects representing the changes between two XML texts, e.g.:
First:
<Customer>
<Id>1</Id>
<FirstName>Angie</FirstName>
<LastName>Jones</LastName>
<ZipCode>23434</ZipCode>
<Contracts>
<Contract>
<Id>234</Id>
<Title>Test Contract 1</Title>
</Contract>
</Contracts>
</Customer>
Second:
<Customer>
<Id>1</Id>
<FirstName>Angie</FirstName>
<MiddleName>S.</MiddleName>
<LastName>Jones-Smith</LastName>
<Contracts>
<Contract>
<Id>234</Id>
<Title>Test Contract 1</Title>
</Contract>
<Contract>
<Id>534</Id>
<Title>Test Contract 2</Title>
</Contract>
</Contracts>
</Customer>
Changes:
Kind: Node: Before After
-------- ---------- -------- ----------------
Change Customer/LastName Jones Jones-Smith
Addition Customer/MiddleName S.
Deletion Customer/ZipCode
Addition Customer/Contracts[1] <Contract>
<Id>534</Id>
<Title>Test Contract 2</Title>
</Contract>
The point is to then pass the changes as a collection of objects onto a GUI which can appropriately display them.
What is the best way to go about this, i.e. structure the class(es) that represents the changes and traverse/examine the XML in order to identify the changes most accurately?
This appears to be a duplicate question for: Xml Comparison in C#
Have a look there, see if it helps.
Quote:
I've got an answer by Martin Honnen in
XML and the .NET Framework MSDN Forum.
In short he suggests to use XQuery
1.0's deep-equal function and supplies some C# implementations. Seems to
work.
For this kind of work I have used XMLDiff patch provided by MSFT. It produces a diffgram which you can then represent as a class if you want.
LINQ to XML has a DeepEquals method that I believe you can utilize in this case.
Then perhaps you can use the XSLT/XQuery processor called SAXON to get the differences.

Categories