How to get inline xml schema? - c#

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();

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 set C# DataTable Locale to be UTC

I create a datatable, and then convert it to a Xml string to send across a network.
The DataTable is created in the US BUT with UTC times in a DateTime column.
When I received it in UK, the timezone appears to be US!
StringWriter sw = new StringWriter();
table.TableName = ZeroMQTopic;
table.WriteXml(sw, XmlWriteMode.WriteSchema);
string tableXML = sw.ToString();
This is the XML:
<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="Test" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Test" msdata:Locale="en-US">
<xs:complexType>
<xs:sequence>
<xs:element name="utcDT" type="xs:dateTime" minOccurs="0" />
<xs:element name="Symbol" type="xs:string" minOccurs="0" />
<xs:element name="Close" type="xs:double" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Test>
<utcDT>2019-11-19T16:08:00-05:00</utcDT> <<<<<<<<< HERE IS THE ISSUE
<Symbol>Widget</Symbol>
<Close>31.25</Close>
</Test>
</NewDataSet>
I think I can set locale to be "en" BUT does that imply UTC or BST?
Or, is there some other way to force this to be UTC?
EDIT 1
table is a subset from a larger table where I have set DateTimeMode to DataSetTimeMode.utc
var table = fullTable.AsEnumerable().Where(x => AllowedSymbols.Contains(x.Field<string>("Symbol"))).CopyToDataTable();
BUT the DateTimeMode is NOT copied!
Also, I cannot set DateTimeMode on table afterwards - as it already has data!

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>

Upload XSD elements in a List

I am Working on Visual-studio 2012 in C#.
I have a xsd file abc.xsd as mentioned below.
I want to add its element names(Class and Place here) in a List.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="DocumentElement" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="DocumentElement" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="PositionMaster">
<xs:complexType>
<xs:all>
<xs:element name="Class" type="xs:string" minOccurs="0" />
<xs:element name="Place" type="xs:string" minOccurs="0" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Code written is below
List listData = new List();
XmlDocument xslDoc = new XmlDocument();
xslDoc.Load(abc.xsd);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xslDoc.NameTable);
nsMgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
What should i write further so that my list contains these xsd elements? These elements may vary in number.
Try this...
using System.Xml.Linq;
XDocument xDoc = XDocument.Load(#"C:\abc.xsd");
var t = xDoc.Descendants().Elements().Attributes().Where(x => x.Name == "name");
You can add more to the where condition to filter out the results as you need

XML DATASET to be Validated

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.

Categories

Resources