XML DATASET to be Validated - c#

Hello all i have a scenario where i have
TWO XML FILES
one xml say "Books.xml" will create the schema for DATASET and i will use other XML file to load data into DATASET and all data in 2nd file will be inserted into DATASET
I have done these things but i m stuck at validating XML i want to validate 2nd xml file
means data entered into dataset should be exactly valid according to DATASET schema which will be according to First XML
i have tried it using XSD but i am not sure if XSD will be able to validate TYPE of data say if i have to input number and i have a string in my xml ,it should throw an exception
i have my xml like this
<DATA>
<HISTORY>
<book1>SOmebook</book1>
</HISTORY>
<POETRY>
<book2>Books</book2>
</POETRY>
</DATA>
and i am generating my XSD Using VisualStudio
to validate it im using a method something like this
XmlReaderSettings settings = //dont know exact settings
string data = null;
XmlReader Reader = XmlReader.Create(File.Open("C:\books.XML", FileMode.Open), settings, data);
DataSet ds = new DataSet();
ds.ReadXml(Reader);

Here's a quick example on how you can use a schema to validate. I'm using the shiporder example from W3Schools.
Schema:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<shiporder orderid="889923">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity> <!--Change to "one" to see validation error-->
<price>9.90</price>
</item>
</shiporder>
And here's the validation using Linq2XML:
XDocument document = XDocument.Load("shiporder.xml");
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create("schema.xsd"));
bool errors = false;
document.Validate(schemas, (o, e) =>
{
Console.WriteLine("Validation error: {0}", e.Message);
errors = true;
});
if (!errors)
{
Console.WriteLine("XML document successfully validated.");
}
else
{
Console.WriteLine("XML document does not validate.");
}
Change the value of of the <quantity> node as mentioned in the comment in order to see how the XML does not validate because of invalid type.

Related

How to add the xmlElement from one xsd file within the xmlElement of another xsd file using XmlSampleGenerator?

I'm using XmlSampleGenerator to generate the xml file from xsd. I'm able to generate the xml file from single xsd file directly using bellow code.
using (var stream = new MemoryStream(File.ReadAllBytes("platform-container.xsd")))
{
var schema = XmlSchema.Read(XmlReader.Create(stream), null);
var gen = new XmlSampleGenerator(schema, new XmlQualifiedName("Document"));
gen.WriteXml(XmlWriter.Create(#"C:\SCIP\SCIP-Phase-1\SCIPAppPrj\XmlFiles\dossier.i6d"));
Console.WriteLine("Autogenerated file is here : C:\\SCIP\\SCIP-Phase-1\\SCIPAppPrj\\XmlFiles\\dossier.i6d");
}
But I'm facing the issue when I have to combine the elements from multiple xsd files and generate one single xml file.
For example my generated xml should look like below:
<?xml version="1.0" encoding="UTF-8"?>
<i6c:Document xmlns:i6c="http://iuclid6.echa.europa.eu/namespaces/platform-container/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://iuclid6.echa.europa.eu/namespaces/platform-container/v1 container.xsd">
<i6c:PlatformMetadata xmlns:i6m="http://iuclid6.echa.europa.eu/namespaces/platform-metadata/v1" xsi:schemaLocation="http://iuclid6.echa.europa.eu/namespaces/platform-container/v1 container.xsd">
<i6m:iuclidVersion>1.0</i6m:iuclidVersion>
<i6m:documentKey>ABC</i6m:documentKey>
<i6m:documentType>DOSSIER</i6m:documentType>
</i6c:PlatformMetadata>
<i6c:Content>
<DOSSIER.SCIP xmlns="http://iuclid6.echa.europa.eu/namespaces/DOSSIER-SCIP/1.0" xmlns:i6="http://iuclid6.echa.europa.eu/namespaces/platform-fields/v1" />
</i6c:Content>
</i6c:Document>
I have main xsd called container.xsd, which has skeleton for my xml file. Also metaData.xsd and content.xsd which contains elements that should be included as a child under one of the element present in container.xsd.
container.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://iuclid6.echa.europa.eu/namespaces/platform-container/v1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://iuclid6.echa.europa.eu/namespaces/platform-container/v1"
elementFormDefault="qualified" attributeFormDefault="qualified">
<xs:element name="Document">
<xs:complexType>
<xs:sequence>
<xs:element name="PlatformMetadata">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:any namespace="##other" processContents="lax" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Content">
<xs:complexType>
<xs:sequence>
<xs:any namespace="##other" processContents="strict" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
metaData.xsd
<xs:schema xmlns="http://iuclid6.echa.europa.eu/namespaces/platform-metadata/v1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://iuclid6.echa.europa.eu/namespaces/platform-metadata/v1"
elementFormDefault="qualified" attributeFormDefault="qualified">
<xs:element name="iuclidVersion" type="xs:string">
</xs:element>
<xs:element name="documentKey" type="xs:string">
</xs:element>
<xs:element name="documentType" type="xs:string">
</xs:element>
</xs:schema>
content.xsd
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://iuclid6.echa.europa.eu/namespaces/DOSSIER-SCIP/1.0" xmlns:ct="http://iuclid6.echa.europa.eu/namespaces/scip/v1" xmlns:i6="http://iuclid6.echa.europa.eu/namespaces/platform-fields/v1" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://iuclid6.echa.europa.eu/namespaces/DOSSIER-SCIP/1.0">
<xs:import namespace="http://iuclid6.echa.europa.eu/namespaces/platform-fields/v1" schemaLocation="platform-fields.xsd"/>
<xs:import namespace="http://iuclid6.echa.europa.eu/namespaces/scip/v1" schemaLocation="commonTypesScipV1.xsd"/>
<xs:element name="DOSSIER.SCIP">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="remarks" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Can anyone suggest me how to handle this using XmlSampleGenerator? How to read the elements from multiple xsd's and genrate single xml file ? How to append the data for each element ?

How to get inline xml schema?

this is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<NewDataSet>
<xs:schema xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema" id="NewDataSet">
<xs:element msdata:IsDataSet="true" msdata:UseCurrentLocale="true" name="NewDataSet">
<xs:complexType>
<xs:choice maxOccurs="unbounded" minOccurs="0">
<xs:element name="Table1">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" msdata:DateTimeMode="Unspecified" name="MAXTIME" type="xs:dateTime"/>
<xs:element minOccurs="0" name="X" type="xs:string"/>
<xs:element minOccurs="0" name="Y" type="xs:string"/>
<xs:element minOccurs="0" name="Z" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Table1>
<MAXTIME/>
<X>123</X>
<Y>DDo</Y>
<Z>Cri</Z>
</Table1>
</NewDataSet>
I want to get the xml schema from the above xml file, however when I use
XmlReader reader = XmlReader.Create(xmlPath);
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchemaInference schema = new XmlSchemaInference();
schemaSet = schema.InferSchema(reader);
it fails:
The supplied xml instance is a schema or contains an inline schema. This class cannot infer a schema for a schema.
so how should I get it to get the schema
Use this:
DataSet dset = new DataSet();
dset.ReadXml("xmlFile.xml",XmlReadMode.IgnoreSchema);
var _schema = dset.GetXmlSchema();

While opening XML file in excel using C# extra columns appear

I am trying to open an xml file in excel, following is the code;
string exeFilePath = System.Windows.Forms.Application.ExecutablePath;
string excelFilePath = Path.GetDirectoryName(exeFilePath);
string path = excelFilePath + "\\shiporder.xml";
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbooks books = excelApp.Workbooks;
Excel.Workbook book = books.OpenXML(path);
Now when I run the above code, I see extra columns inside the excel file in the start, I am using Microsoft.Office.Interop.Excel;
The following is the XML file;
<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
The following is the XSD file;
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
Kindly tell me how can this issue be solved?
Currently the excel file looks like this;
/shiporder
/#orderid /#orderid/#agg /#xsi:noNamespaceSchemaLocation /item/note /item/price /item/price/#agg /item/quantity /item/quantity/#agg /item/title /orderperson /shipto/address /shipto/city /shipto/country /shipto/name
889923 889923 shiporder.xsd Special Edition 10.9 10.9 1 1 Empire Burlesque John Smith Langgt 23 4000 Stavanger Norway Ola Nordmann
889923 shiporder.xsd 9.9 9.9 1 1 Hide your heart John Smith Langgt 23 4000 Stavanger Norway Ola Nordmann

C# XML not correctly validating against Schema in XmlReaderSettings

I searched and did not find any questions addressing this problem.
I am attempting to validate various XML against a schema and it seems to be validating ALL well-formed XML, instead of just XML that conforms to the schema. I have posted the code I am using, the Schema, a sample valid XML and a sample invalid XML.
I have been struggling with this for awhile. I am in the dark on most of this. I've had to learn how to write an XSD, write the XSD, then learn how to parse XML in C#. None of which I have ever done before. I have used many tutorials and the microsoft website to come up with the following. I think this should work, but it doesn't.
What am I doing wrong?
private bool ValidateXmlAgainstSchema(string sourceXml, string schemaUri)
{
bool validated = false;
try
{
// REF:
// This should create a SCHEMA-VALIDATING XMLREADER
// http://msdn.microsoft.com/en-us/library/w5aahf2a(v=vs.110).aspx
XmlReaderSettings xmlSettings = new XmlReaderSettings();
xmlSettings.Schemas.Add("MySchema.xsd", schemaUri);
xmlSettings.ValidationType = ValidationType.Schema;
xmlSettings.ValidationFlags = XmlSchemaValidationFlags.None;
XmlReader xmlReader = XmlReader.Create(new StringReader(sourceXml), xmlSettings);
// parse the input (not sure this is needed)
while (xmlReader.Read()) ;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlReader);
validated = true;
}
catch (XmlException e)
{
// load or parse error in the XML
validated = false;
}
catch (XmlSchemaValidationException e)
{
// Validation failure in XML
validated = false;
}
catch (Exception e)
{
validated = false;
}
return validated;
}
The XSD / Schema. The intent is to accept XML that contains either an Incident or a PersonOfInterest.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="MySchema.xsd"
xmlns="MySchema.xsd"
elementFormDefault="qualified"
>
<xs:element name="Incident" type="IncidentType"/>
<xs:element name="PersonOfInterest" type="PersonOfInterestType"/>
<xs:complexType name="IncidentType">
<xs:sequence>
<xs:element name="Description" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="PersonOfInterest" type="PersonOfInterestType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PersonOfInterestType">
<xs:sequence>
<xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Here is a sample of valid XML
<?xml version="1.0" encoding="utf-8" ?>
<Incident
xmlns="MySchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com MySchema.xsd"
>
<Description>something happened</Description>
<PersonOfInterest>
<Name>Joe</Name>
</PersonOfInterest>
<PersonOfInterest>
<Name>Sue</Name>
</PersonOfInterest>
</Incident>
This is a sample of well-formed invalid XML which should throw an exception (I thought), but when I try it, the code returns true, indicating it is valid against the schema.
<ghost>Boo</ghost>
The reason your <ghost>Boo</ghost> validates is that the parser cannot find any schema matching the xml. If there is no schema then the parser assumed validity, providing the xml is well-formed. It's counter-intuitive I know, and will probably differ based on parser implementation.
This notwithstanding, there are several problems with your code:
Two Root Elements
This is a big no-no in xsd - you can only have a single root element. Some parsers will actually throw an exception, others tolerate it but will only use the first root element (in your case Incident) for any subsequent validation.
Use of schemaLocation attribute
This should take the value (namespace) (URI) where the namespace is the targetNamespace of the schema and the URI is the location of the schema. In your case you appear to be using the schema file name as your target namespace. Additionally, looking at your code, you are loading the schema into your xml reader so you don't actually need the schemaLocation attribute at all. This is an optional attribute and some parsers completely ignore it.
I would suggest the following changes:
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://MyMoreMeaningfulNamespace"
xmlns="http://MyMoreMeaningfulNamespace"
elementFormDefault="qualified"
>
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Incident" type="IncidentType"/>
<xs:element maxOccurs="unbounded" name="PersonOfInterest" type="PersonOfInterestType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="IncidentType">
<xs:sequence>
<xs:element name="Description" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="PersonOfInterest" type="PersonOfInterestType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PersonOfInterestType">
<xs:sequence>
<xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Which validates this instance
<Root xmlns="http://MyMoreMeaningfulNamespace">
<Incident>
<Description>something happened</Description>
<PersonOfInterest>
<Name>Joe</Name>
</PersonOfInterest>
<PersonOfInterest>
<Name>Sue</Name>
</PersonOfInterest>
</Incident>
<Incident>
...
</Incident>
<PersonOfInterest>
<Name>Manny</Name>
</PersonOfInterest>
<PersonOfInterest>
...
</PersonOfInterest>
</Root>

reading xml file incorrectly, return value 0 when in file is 1

I'm creating a xml file with the variable
verificacaoconjunta
This is a piece of the xml file
// <tabelaGERAL>
// <Nome>Verificacao conjunta</Nome>
// <Valor>1</Valor>
// <tabelaGERAL>
it states clearly that the value of it is 1
but when i read the table as in here:
DataTable lertabela = new DataTable();
lertabela.ReadXml(path);
verificacaoconjunta = lertabela.Rows[4]["valor"].ToString();
it suprisisingly returns 0, i created a button to check for it's value on the table and here's the result
http://puu.sh/8tkmC.png
any hint on what might be wrong?
EDIT:
The full xml file
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="tabelaGERAL" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="tabelaGERAL">
<xs:complexType>
<xs:sequence>
<xs:element name="Nome" type="xs:string" minOccurs="0" />
<xs:element name="Valor" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<tabelaGERAL>
<Nome>Accao G</Nome>
<Valor>10</Valor>
</tabelaGERAL>
<tabelaGERAL>
<Nome>Accao Q</Nome>
<Valor>15</Valor>
</tabelaGERAL>
<tabelaGERAL>
<Nome>Accao W</Nome>
<Valor>7</Valor>
</tabelaGERAL>
<tabelaGERAL>
<Nome>Tipo de verificacao</Nome>
<Valor>1</Valor>
</tabelaGERAL>
<tabelaGERAL>
<Nome>Verificacao conjunta</Nome>
<Valor>1</Valor>
</tabelaGERAL>

Categories

Resources