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
Related
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 ?
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!
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();
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>
I want to read a section from the XML file below with C#.
<?xml version="1.0" encoding="utf-8" >
<DataSet>
<xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="Column1" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<NewDataSet>
<Table diffgr:id="Table1" msdata:rowOrder="0">
<Column1><Properties><Property>.....
I want to extract the nodes below the Column1 nodes. The Properties node has lots of Property nodes, so I want the Properties node with all the Property nodes.
Please let me know the easiest and efficient way to get the nodes in C#.
You can use an Linq-to-XML classes to parse the string and then XPath expression to select the nodes that you want:
XElement doc = XElement.Parse(s); //where s is a string containing the XML
var properties = doc.XPathSelectElements("//Column1/Properties");
Now properties contains a enumerable of the nodes that you want.
If you want to iterate through all the Property nodes you can do so like this:
foreach(var pp in properties)
{
foreach(var p in pp.Elements("Property"))
{
//do something
}
}