How to use namespaces when using XSD to validate a XML file - c#

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.

Related

How to reference XSD schema correctly? [duplicate]

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>

Loading XML with XDocument when xsi:nil

I am trying to read this XML document with XDocument in c sharp.
<Instrument_Root>
<Instrument_ID>123</Instrument_ID>
<Deal_number xsi:nil="true"/>
</Instrument_Root>
I use the below code to read the XML doc:
XDocument xDoc = XDocument.Load("XMLFile1.xml");
I receive an error message because of the xsi:nil
Error merssage:An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll 'xsi' not declared.
Is there a way to treat xsi as NULL?
Thank you
The namespace prefix xsi has to be declared somewhere for the document to be qualified as XML, for example :
<Instrument_Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Instrument_ID>123</Instrument_ID>
<Deal_number xsi:nil="true"/>
</Instrument_Root>
You can try to change the XML files to look like this:
<Instrument_Root xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<Instrument_ID>123</Instrument_ID>
<Deal_number xsi:nil="true"/>
</Instrument_Root>
screencapture

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.

How to read nested XML using xDocument in Silver light?

Hi currently I have a nested XMl , having the following Structure :
<?xml version="1.0" encoding="utf-8" ?>
<Response>
<Result>
<item id="something" />
<price na="something" />
<?xml version="1.0" encoding="UTF-8" ?>
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/">
</Result>
<NumberReturned>10</NumberReturned>
<TotalMatches>10</TotalMatches>
</Response>
Any help on how to read this using Xdocument or XMLReader will be really helpfull.
Thanks,
Subhendu
XDocument and XmlReader are both XML parsers that expect a properly formed XML as input. What you have shown is not a XML file. So the first task would be to extract the nested XML and as this is not valid XML you cannot rely on any parser to do this job. You'll need to resort to string manipulation and or regular expressions.
My suggestion would be to fix the procedure generating this invalid XML in the first place. Another suggestion is to never generate a XML file manually but use an appropriate tool for this (XmlWriter, XDocument, ...)

Categories