How to reference XSD schema correctly? [duplicate] - c#

I found some tips for this problem, but still didn't help me.
Here is my XML
<?xml version="1.0" encoding="UTF-8"?>
<work xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.w3.org/2001/XMLSchema-instance"
tns:schemaLocation="myXSDSchema.xsd">
<tns:Objects>
<tns:Object Name=":" Location=":">
</tns:Object>
</tns:Objects>
</work>
Here is my XSD file:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns = "http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
(some checks)
</schema>
My XSD file is located in the same folder as the XML.
How to link these 2 files?

How to link an XSD to an XML document depends upon whether the XML document is using namespaces or not...
Without namespaces
Use xsi:noNamespaceSchemaLocation to provide a hint as to the XSD to be used:
XML
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="example.xsd">
<!-- ... -->
</root>
XSD
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<!-- ... -->
</xsd:element>
</xsd:schema>
With namespaces
Use xsi:schemaLocation to provide a hint as to the XSD to be used:
XML
<ns:root xmlns:ns="http://example.com/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com/ns example-ns.xsd">
<!-- ... -->
</ns:root>
XSD
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/ns">
<xsd:element name="root">
<!-- ... -->
</xsd:element>
</xsd:schema>

Related

VS Generated WSDL Namespace Issues

Good Afternoon,
We have been trying to consume a Carrier API, but have hit some issue with the WSDL Generation. The Example XML message the company has provided has ns1-3 and appears that it is important for the address fields. the XML generated from their WSDL seems to generate the same fields but has no name spaces like the original and fails with "Address details are invalid", Is there some setting we are missing so that it generates the correct XML?
This is their Example XML
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns3:CreateLabel xmlns:ns3="http://courier.ck.dx.metafour.com/" xmlns:ns2="http://www.thedx.co.uk/eai/canonical/types/v2.0">
<order>
<ns2:customerID>14337622</ns2:customerID>
<ns2:dates>
<date format="yyyy-MM-dd HH:mm:ss" type="requestedCollectionDate">2018-12-12 17:59:21</date>
</ns2:dates>
<sourceSystemReference xmlns="http://www.thedx.co.uk/eai/canonical/types/v2.0">AMS207554</sourceSystemReference>
<customerReference xmlns="http://www.thedx.co.uk/eai/canonical/types/v2.0">286956</customerReference>
<orderAttributes xmlns="http://www.thedx.co.uk/eai/canonical/types/v2.0" xsi:nil="true" />
<ns2:orderLines>
<ns2:consignment>
<pieces>
<dimensions>
<value>1.0</value>
<type>cdlWeight</type>
<UOM>KG</UOM>
</dimensions>
<barcode xsi:nil="true" />
<trackingNumber xsi:nil="true" />
</pieces>
<qty>1</qty>
<legacyService>
<name>serviceLevel</name>
<partyId>0</partyId>
<partyType>HITS</partyType>
</legacyService>
<legacyService>
<name>serviceType</name>
<partyId>2</partyId>
<partyType>HITS</partyType>
</legacyService>
<deliverTo>
<ns2:address primary="true">
<organisationName>Argos</organisationName>
<addressLine1>Argos</addressLine1>
<addressLine2>Argos Ltd</addressLine2>
<addressLine3>11 Canning Street</addressLine3>
<city>BURNLEY</city>
<postalCode>BB12 0AD</postalCode>
<country>
<countryCode>GB</countryCode>
<description>GB</description>
</country>
</ns2:address>
<contact />
</deliverTo>
</ns2:consignment>
</ns2:orderLines>
<labelType>ZPL</labelType>
</order>
<serviceHeader>
<password>test</password>
<userId>test</userId>
</serviceHeader>
</ns3:CreateLabel>
</s:Body>
</s:Envelope>
This is our Generated XML
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CreateLabel xmlns="http://courier.ck.dx.metafour.com/">
<order xmlns="">
<customerID xmlns="http://www.thedx.co.uk/eai/canonical/types/v2.0">14337622</customerID>
<dates xmlns="http://www.thedx.co.uk/eai/canonical/types/v2.0">
<date xmlns="" format="yyyy-MM-dd HH:mm:ss" type="requestedCollectionDate">2019-01-25 10:17:33</date>
</dates>
<sourceSystemReference xmlns="http://www.thedx.co.uk/eai/canonical/types/v2.0">AMS207554</sourceSystemReference>
<customerReference xmlns="http://www.thedx.co.uk/eai/canonical/types/v2.0">286956</customerReference>
<orderAttributes xmlns="http://www.thedx.co.uk/eai/canonical/types/v2.0" xsi:nil="true" />
<orderLines xmlns="http://www.thedx.co.uk/eai/canonical/types/v2.0">
<consignment>
<pieces xmlns="">
<dimensions>
<value>1.0</value>
<type>cdlWeight</type>
<UOM>KG</UOM>
</dimensions>
<barcode xsi:nil="true" />
<trackingNumber xsi:nil="true" />
</pieces>
<qty>1</qty>
<legacyService xmlns="">
<name>serviceLevel</name>
<partyId>0</partyId>
<partyType>HITS</partyType>
</legacyService>
<legacyService xmlns="">
<name>serviceType</name>
<partyId>2</partyId>
<partyType>HITS</partyType>
</legacyService>
<deliverTo xmlns="">
<address xmlns="http://www.thedx.co.uk/eai/canonical/types/v2.0">
<organisationName xmlns="">Argos</organisationName>
<addressLine1 xmlns="">Argos</addressLine1>
<addressLine2 xmlns="">Argos Ltd</addressLine2>
<addressLine3 xmlns="">11 Canning Street</addressLine3>
<city xmlns="">BURNLEY</city>
<postalCode xmlns="">BB12 0AD</postalCode>
<country xmlns="">
<countryCode>GB</countryCode>
<description>GB</description>
</country>
</address>
<contact xmlns="http://www.thedx.co.uk/eai/canonical/types/v2.0" />
</deliverTo>
</consignment>
</orderLines>
<labelType>ZPL</labelType>
</order>
<serviceHeader xmlns="">
<password>test</password>
<userId>test</userId>
</serviceHeader>
</CreateLabel>
</s:Body>
</s:Envelope>
Any help would be greatly appreciated!
This is resolved,
I could not figure out a way to change or add additional namespace to the generated references.cs file, and ended up sending the SOAP to their server manually with an XML string serialized using a httpWebRequest and XmlSerializer

How to use namespaces when using XSD to validate a XML file

I am trying to understand how I can validate an XML file with an XSD.
I don't quite understand the namespaces.
If I have the code:
XmlDocument xDoc = new XmlDocument();
xDoc.Load(file.InputStream);
xDoc.Schemas.Add("http://www.w3.org/2001/XMLSchema-instance", #"C:MyXSD.xsd");
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
xDoc.Validate(eventHandler);
I get the Error:
"The target namespace of an attribute declaration, whether local or global, must not match http://www.w3.org/2001/XMLSchema-instance."
What should I be setting the namespace to when I add the schema to the xDoc?
I have an xml like this:
<?xml version="1.0"?>
<rootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
// My Nodes
</rootNode>
and I created an XSD from that XML.
<?xml version="1.0" encoding="Windows-1252"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xsd:element name="rootNode">
// My Nodes
</xsd:element>
</xs:schema>
In your scenario you can just pass null as the namespace to Schemas.Add, as you are not actually using namespaces, i.e. your XML document does not use a namespace (which would be specified either with the xmlns attribute or the use of a namespace prefix).
For more information on namespaces please see http://www.w3schools.com/xml/xml_namespaces.asp.
Note that http://www.w3.org/2001/XMLSchema-instance is a namespace used to define schema attributes that relates only to the schema infrastructure and not your specific XML schema.

how to reference xsd file from xml

1) pls.xsd file
I have included pls.xsd in xml.xsd in same folder
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Externals changed by QTAssistant (http://www.paschidev.com) -->
<!--
This is a draft schema for the XML language defined in the
Pronunciation Lexicon Specification
(latest version at <http://www.w3.org/TR/pronunciation-lexicon/>)
At the time of writing, the specification as well as this schema are
subject to change, and no guarantee is made on their accuracy or the fact
that they are in sync.
Last modified: $Date: 2007/12/11 12:08:40 $
Copyright รป 2006 World Wide Web Consortium, (Massachusetts Institute
of Technology, ERCIM, Keio University). All Rights Reserved. See
http://www.w3.org/Consortium/Legal/.
-->
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:p="http://www.w3.org/2005/01/pronunciation-lexicon"
targetNamespace="http://www.w3.org/2005/01/pronunciation-lexicon"
elementFormDefault="qualified" version="1.0">
<xs:annotation>
<xs:documentation>Importing dependent namespaces</xs:documentation>
</xs:annotation>
<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="xml.xsd" />
...
</xs:schema>
2)My XML file
from this file i am referencing pls.xsd
<?xml version="1.0" encoding="utf-8"?>
<lexicon version="1.0"
xmlns="http://www.w3.org/2005/01/pronunciation-lexicon"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon
file://C:/xsdforproject/pls.xsd"
alphabet="x-microsoft-ups" xml:lang="en-IN">
<lexeme>
</lexeme>
</lexicon>
I have above two codes these giving me an errors in both my XML and in my pls.xsd file,
an error has occurred while opening external "DTD" file:///C:/xsdforproject/XMLSchema.dtd': Could not find file 'C:\xsdforproject\XMLSchema.dtd
I am using "Visual Studio 2010".
How to resolve this issue?
It would appear that something you're using is referencing XMLSchema.dtd, so have you tried downloading it and placing it in your xsdforproject folder?
http://www.w3.org/2009/XMLSchema/XMLSchema.dtd

C# XmlDocument is not being updated with respective XSD default attributes

Here is an example from a xsd and a xml
XML
<?xml version="1.0" encoding="UTF-8"?>
<config test2="true">
</config>
XSD
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="rules.xsd" xmlns="rules.xsd" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
<xs:element name="config">
<xs:complexType>
<xs:attribute name="test1" type="xs:boolean" default="false" />
<xs:attribute name="test2" type="xs:string" default="mary123" />
</xs:complexType>
</xs:element>
</xs:schema>
I can use this xsd to validate this xml in C# using this block of code
XmlDocument doc = new XmlDocument();
XmlTextReader schemaReader = new XmlTextReader(System.IO.Path.GetFullPath("Mi_XSD_here.xsd"));
XmlSchema schema = XmlSchema.Read(schemaReader, ValidationCallBack);
doc.Schemas.Add(schema);
doc.Load("Mi_XML_here.xml");
doc.Validate(ValidationCallBack);
The problem is: I have two default attributes in xsd, but when i run this code he doesn't insert the attributes in XmlDocument, the result is the same xml that i passed to the system.
The default attributes are not working and i cant figure why they aren't working, i do believe that exists other forms to solve this problem, i did find this but didn't worked
Extensions.Validate Method
obs: ValidationCallBack is some return on error function that i think isn't related to the problems
Your schema's target namespace is rules.xsd so your xml file needs to include that to validate against the schema. Also I'm assuming teste2 is a typo (since that doesn't conform to the schema) and you meant test2:
<config test2="" xmlns="rules.xsd" />
In this case only test1 will get added with a default since test2 is already set to an empty string.

Why am I getting org.xml.sax.SAXException for this block of XML?

I've anonymised a bit of this. Hopefully it doesn't detract from the useful message. stringARRAY is where I think I'm getting thrown at, but I'm not sure, so if I'm looking at the wrong place, lemme know.
From the WSDL
<xsd:element name="LongishOpName">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="stringARRAY" type="xsd:string" />
<xsd:element name="stringfield1" type="xsd:string" />
<xsd:element name="stringfield2" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Actual method call looks like this:
string originalValue = "some useful value";
string[] usefulName1 = new[] { originalValue };
service.response[] responses = server.LongishOpName( usefulName1, someString1, someString2 );
And it generates this XML to the server (Thanks to Fiddler2):
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<LongishOpName xmlns="http://appropriate-namespace">
<stringARRAY>114003</stringARRAY>
<stringfield1>a string</stringfield1>
<stringfield2>a string</stringfield2>
</LongishOpName>
</soap:Body>
</soap:Envelope>
To which I get this response
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Server.userException</faultcode>
<faultstring>org.xml.sax.SAXException: Found character data inside an array element while deserializing</faultstring>
<detail>
<ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">SERVERNAME</ns1:hostname>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
Is my soap message actually coming out malformed? (yes I am using the ASP.NET web references to do all the lifting, I'm not generating by hand)
Should I be doing something different?
Do I just need to go home and sleep on it and tomorrow everything will "just work"?
So the answer to this question was: the WSDL does not match what the actual method does. I don't know how this is exactly.
For clarification, the method takes an array of three strings, not an array of strings and then two separate strings. So the takeaway is "doublecheck the WSDL is accurate".
I'm guessing it has to do with namespaces not being treated properly.
I always find it helpful to cut & paste the error messages I get into Google and see if someone else has had the same problem. I don't always find an answer, but I usually learn that I'm neither alone nor the first.

Categories