XSD relation between classes, + hierarchy relation - c#

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>

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 to get key value pairs from resx file using powershell script?

I need to obtain values from c# resx file using powershell script.Is it possible to obtain these xml values using only PS scripting?I wrote a code only in PS but it didn't work.Snippets would help!!
Using ResXResourceSet Class:
$File = 'c:\path\to\file.resx'
# Add assembly containing class, not sure if needed, but wouldn't hurt
Add-Type -AssemblyName System.Windows.Forms
# Create new ResXResourceSet, pass path to the resx file to the constructor
$ResourceSet = New-Object -TypeName 'System.Resources.ResXResourceSet' -ArgumentList $File
# Get resources
$ResourceSet.GetString('TestString')
$ResourceSet.GetString('AnotherTestString')
ResX example:
<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="TestString" xml:space="preserve">
<value>This is some test string...</value>
</data>
<data name="AnotherTestString" xml:space="preserve">
<value>Another test string...</value>
</data>
</root>
Output:
This is some test string...
Another test string...
P.S.
ResX files are XML, so you can get values from them using XPath or other methods.
References:
How to clean up resx files with PowerShell

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>

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>

Merge two xsd file in one xsd with 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>

Categories

Resources