Merge two xsd file in one xsd with c# - c#

i want to merge two xsd files to one xsd using c#.
how can i do it with c#? can anyone help me?

You may be looking for <xsd:import /> or <xsd:include /> see the MSDN documentation for differences and restrictions.
Your main schema document
Main.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import schemaLocation="Imported.xsd" />
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="localElement" />
<xsd:element ref="importedElement" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Your imported schema document
Imported.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="importedElement">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="someElement" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Related

When adding Schema to XmlSchemaSet, I get an exception

I have this XSD I made
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:complexType name="FunnyType">
<xsd:element name="Prueba1" type="xsd:string"/>
<xsd:element name="Prueba2" type="xsd:int"/>
</xsd:complexType>
<xsd:element name="Funnys">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="funny" type="FunnyType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Which gives me a System.Xml.Schema.XmlSchemaException: 'http://www.w3.org/2001/XMLSchema:element' is invalid in this context. (not the exact error, I translated it from spanish).
I have gone up and down everywhere (here, asking colleagues, etc) and made changes to the file, yet I cannot find the error.
What am I lacking here that makes c# throw an exception?
Thank you
You can't have this:
<xsd:complexType name="FunnyType">
<xsd:element name="Prueba1" type="xsd:string" />
<xsd:element name="Prueba2" type="xsd:int" />
</xsd:complexType>
You have to begin the complexType with one of the 3 compositors: xsd:sequence, xsd:all or xsd:choice:
For example:
<xsd:complexType name="FunnyType">
<!-- Can also be xsd:all or xsd:choice -->
<xsd:sequence>
<xsd:element name="Prueba1" type="xsd:string" />
<xsd:element name="Prueba2" type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
You need to decide which one to use that best suits what your complex type is meant to model.

How do XML Schema namespaces map to C# class namespaces?

For an XSD document such as this, say I was to create a C# class using some tool like xsd.exe or xsd2code, How do I define the namespace each class belongs to? Or do they all belong to only one namespace?
?xml version="1.0" encoding="UTF-8"?><xsd:schema targetNamespace="http://com.ibm.wbit.comptest.controller"
xmlns:Q1="http://com.ibm.wbit.comptest.controller" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="TestResults">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="testSuites" type="Q1:TestSuiteRun"/>
</xsd:sequence>
<xsd:attribute name="testProject" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="TestSuiteRun">
<xsd:complexContent>
<xsd:extension base="Q1:TestRun">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="testCases" type="Q1:TestCaseRun">
</xsd:element>
</xsd:sequence>
<xsd:attribute name="tests" type="xsd:int"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="TestCaseRun">
<xsd:complexContent>
<xsd:extension base="Q1:TestRun">
<xsd:sequence>
<xsd:element name="result" type="Q1:Severity"/>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="variations" type="Q1:VariationRun">
</xsd:element>
</xsd:sequence>
<xsd:attribute name="variationCount" type="xsd:int"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="VariationRun">
<xsd:complexContent>
<xsd:extension base="Q1:TestRun">
<xsd:sequence>
<xsd:element name="result" type="Q1:Severity"/>
<xsd:element name="exception" nillable="true" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
This element is used to display the exception of a failure or error.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:simpleType name="Severity">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="pass"/>
<xsd:enumeration value="fail"/>
<xsd:enumeration value="error"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="TestRun">
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="startTime" type="xsd:dateTime"/>
<xsd:attribute name="endTime" type="xsd:dateTime"/>
<xsd:attribute name="result" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>

XSD relation between classes, + hierarchy relation

I have some xml in the following format where in the group state can either be states of true false as defined in definition state, or use another group with one of it's values.
How can I use xsd for this relation contsraint - using ID and IDref solved for the definition and group name, but how do I constrain that the value of the group is really a value as defined in the group?
Also is there any way to distinguish between the ID, in case using definition the value should be true / false string. in case using another group state should be one of its defined state?
<Definitions>
<Definition Name="Name1"/>
<Definition Name="Name2" />
</Definitions>
<ValueGrps>
<Group Name="grp1">
<ComplexState Name="state1">
<State Name="Name1" Value="true" />
<State Name="Name2" Value="true" />
</ComplexState>
<ComplexState Name="state2">
<State Name="Name1" Value="false" />
<State Name="Name2" Value="true" />
</ComplexState>
</Group>
<Group Name="grp2">
<ComplexState Name="state1">
<State Name="grp1" Value="state1" />
</ComplexState>
</Group>
</ValueGrps>
XSD 1.0 alone cannot help you here. You could apply Schematron for sure; there is an XSLT 1.0 implementation which works on .NET (since you've indicated C#).
XSD 1.1 has built in support for what you need. However, XSD 1.1 is poorly supported on .NET as in you either buy Saxon's EE for NET OR you use IKVM to "port" XercesJ on .NET (I've done it and it works). You would have issues, in an open environment, to have other people using XSD 1.1 schemas.
If this is a new design, I would probably also change some of the design... for example, a State referencing a group is not the same as a State referencing a Definition - I would then define different content models for the different reference types. I would also drop ID/IDREF in favour of key(unique)/keyref. I am attaching an example below which would work with the provided XML.
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Definitions">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="Definition">
<xsd:complexType>
<xsd:attribute name="Name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ValueGrps">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="Group">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="ComplexState">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="State">
<xsd:complexType>
<xsd:attribute name="Name" type="xsd:string" use="required" />
<xsd:attribute name="Value" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="Name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="Name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="PK_1">
<xsd:selector xpath="Definitions/Definition|ValueGrps/Group"/>
<xsd:field xpath="#Name"/>
</xsd:key>
<xsd:keyref name="FK_1" refer="PK_1">
<xsd:selector xpath="ValueGrps/Group/ComplexState/State"/>
<xsd:field xpath="#Name"/>
</xsd:keyref>
</xsd:element>
</xsd:schema>

Zillow's SearchResults.xsd and Visual Studio's XSD command

I tried to run Visual Studio's XSD.EXE utility on the zillow "SearchResults.xsd" to generate c# or vb.net classes but am having no luck and am looking for help. I keep getting errors (see below). Here is the original file I downloaded from http://www.zillow.com/howto/api/GetDeepSearchResults.htm:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified"
elementFormDefault="unqualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.zillow.com/static/xsd/SearchResults.xsd"
xmlns:ZillowTypes="http://www.zillow.com/static/xsd/ZillowTypes.xsd">
<xsd:import namespace="http://www.zillow.com/static/xsd/ZillowTypes.xsd"
schemaLocation="/vstatic/4/static/xsd/ZillowTypes.xsd" />
<xsd:element name="searchresults">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="request">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="address" type="xsd:string" />
<xsd:element name="citystatezip" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="message" type="ZillowTypes:Message" />
<xsd:element minOccurs="0" name="response">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="results">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="unbounded" name="result" type="ZillowTypes:SimpleProperty" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Here are the errors I keep getting when I try XSD.EXE from Visual Studio 2010:
C:\Users\username\Documents>xsd /classes SearchResults.xsd
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Schema validation warning: Type 'http://www.zillow.com/static/xsd/ZillowTypes.xsd:Message' is not declared. Line 25, position 6.
Schema validation warning: Type 'http://www.zillow.com/static/xsd/ZillowTypes.xsd:SimpleProperty' is not declared. Line 33, position 12.
Warning: Schema could not be validated. Class generation may fail or may produce
incorrect results.
Error: Error generating classes for schema 'SearchResults'.
- The datatype 'http://www.zillow.com/static/xsd/ZillowTypes.xsd:Message' is missing.
It looks like everything is defined in the ZillowTypes.xsd so I am stumped.
There are a couple of problems with the setup. Starting from the location of the XSD file, which is supposed to be at http://www.zillow.com/static/xsd/SearchResults.xsd
The above XSD is referencing another xsd using an import with a relative uri /vstatic/4/static/xsd/ZillowTypes.xsd which resolves to http://www.zillow.com/vstatic/4/static/xsd/ZillowTypes.xsd - but this URL is not dereferenceable (404)!!
If instead you try http://www.zillow.com/static/xsd/ZillowTypes.xsd then you found part 2 of your puzzle, which is ZillowTypes.xsd.
Because of these errors, you need to download both XSD files to your local machine to fix them.
Download first as SearchResults.xsd and second as ZillowTypes.xsd; put them in the same folder.
Edit SearchResults.xsd by changing the line below:
<xsd:import namespace="http://www.zillow.com/static/xsd/ZillowTypes.xsd" schemaLocation="/vstatic/4/static/xsd/ZillowTypes.xsd" />
to:
<xsd:import namespace="http://www.zillow.com/static/xsd/ZillowTypes.xsd" schemaLocation="ZillowTypes.xsd" />
Then you run into another problem. ZillowTypes.xsd contains an invalid element declaration! So edit the decl below:
<xsd:complexType name="investmentBuyingBlock">
<xsd:sequence>
<xsd:element name="buying" minOccurs="0" maxOccurs="unbounded">
<xsd:sequence>
<xsd:element name="year" type="xsd:integer"/>
<xsd:element name="rentalIncome" type="xsd:integer"/>
<xsd:element name="otherIncome" type="xsd:integer"/>
<xsd:element name="mortgagePayment" type="xsd:integer"/>
<xsd:element name="principal" type="xsd:integer"/>
<xsd:element name="interest" type="xsd:integer"/>
<xsd:element name="hoaFees" type="xsd:integer"/>
<xsd:element name="propertyTaxes" type="xsd:integer"/>
<xsd:element name="utilities" type="xsd:integer"/>
<xsd:element name="renovations" type="xsd:integer"/>
<xsd:element name="maintainCosts" type="xsd:integer"/>
<xsd:element name="homeOwnerInsurance" type="xsd:integer"/>
<xsd:element name="managementFees" type="xsd:integer"/>
<xsd:element name="advertisingCosts" type="xsd:integer"/>
<xsd:element name="otherExpenses" type="xsd:integer"/>
<xsd:element name="totalExpenses" type="xsd:integer"/>
<xsd:element name="opportunityCostInitial" type="xsd:integer"/>
<xsd:element name="opportunityCostYearly" type="xsd:integer"/>
<xsd:element name="depreciationBuilding" type="xsd:integer"/>
<xsd:element name="depreciationrenovation" type="xsd:integer"/>
<xsd:element name="totalBenefit" type="xsd:integer"/>
<xsd:element name="totalProfitLoss" type="xsd:integer"/>
</xsd:sequence>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
To:
<xsd:complexType name="investmentBuyingBlock">
<xsd:sequence>
<xsd:element name="buying" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="year" type="xsd:integer"/>
<xsd:element name="rentalIncome" type="xsd:integer"/>
<xsd:element name="otherIncome" type="xsd:integer"/>
<xsd:element name="mortgagePayment" type="xsd:integer"/>
<xsd:element name="principal" type="xsd:integer"/>
<xsd:element name="interest" type="xsd:integer"/>
<xsd:element name="hoaFees" type="xsd:integer"/>
<xsd:element name="propertyTaxes" type="xsd:integer"/>
<xsd:element name="utilities" type="xsd:integer"/>
<xsd:element name="renovations" type="xsd:integer"/>
<xsd:element name="maintainCosts" type="xsd:integer"/>
<xsd:element name="homeOwnerInsurance" type="xsd:integer"/>
<xsd:element name="managementFees" type="xsd:integer"/>
<xsd:element name="advertisingCosts" type="xsd:integer"/>
<xsd:element name="otherExpenses" type="xsd:integer"/>
<xsd:element name="totalExpenses" type="xsd:integer"/>
<xsd:element name="opportunityCostInitial" type="xsd:integer"/>
<xsd:element name="opportunityCostYearly" type="xsd:integer"/>
<xsd:element name="depreciationBuilding" type="xsd:integer"/>
<xsd:element name="depreciationrenovation" type="xsd:integer"/>
<xsd:element name="totalBenefit" type="xsd:integer"/>
<xsd:element name="totalProfitLoss" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
The re-run you XSD command line by replacing
SearchResults.xml
with
SearchResults.xsd ZillowTypes.xsd
It should work (at least the XSDs are valid now).

Avoid sending object properties not set on an auto-generated class from xml serializer

I have imported a WSDL from a third party Java web service. One of the methods requires a ComplexType object, that was serialized into a class on C# when I imported the WSDL. The WSDL is as follows:
<xsd:complexType name="MethodReturn">
<xsd:sequence>
<xsd:element name="param1" nillable="true" type="xsd:string" />
<xsd:element name="param2" nillable="true" type="xsd:string" />
<xsd:element name="param3" nillable="true" type="xsd:string" />
<xsd:element name="param4" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
When I send the Method, I'm getting the error below:
org.xml.sax.SAXException: Invalid element in com.teste.webservice.model.MethodReturn - param3
The third party developer told me that I shouldn't send the param3 property along the serialized XML when I send the method. How can I accomplish this using C#? If I set MethodReturn=null, the property is included as xsil:nil="true", as follows:
<param1 xsi:type="xsd:string">U</param1>
<param2 xsi:type="xsd:string">2032290</param2>
<param3 xsi:nil="true"/>
<param4 xsi:type="xsd:string">EU1</param4>
What I would like to do is not to send param3 at all, like below. Is that possible?
<param1 xsi:type="xsd:string">U</param1>
<param2 xsi:type="xsd:string">2032290</param2>
<param4 xsi:type="xsd:string">EU1</param4>
Tks
Well either the 3rd party developer or the WSDL is wrong. If the WSDL is wrong, then edit it:
<xsd:complexType name="MethodReturn">
<xsd:sequence>
<xsd:element name="param1" nillable="true" type="xsd:string" />
<xsd:element name="param2" nillable="true" type="xsd:string" />
<xsd:element name="param3" type="xsd:string" />
<xsd:element name="param4" nillable="true" type="xsd:string" />
</xsd:sequence>

Categories

Resources