I have a datatable and I wanna export it to XML file. In my XML file, I want it have schema, as this format :
-<NewDataSet>
-<xs:schema xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" id="NewDataSet">
-<xs:element msdata:UseCurrentLocale="true" msdata:IsDataSet="true" name="NewDataSet">
-<xs:complexType>
-<xs:choice maxOccurs="unbounded" minOccurs="0">
-<xs:element name="Table1">
-<xs:complexType>
-<xs:sequence>
<xs:element name="MA_NVGCS" minOccurs="0" type="xs:string"/>
<xs:element name="MA_KHANG" minOccurs="0" type="xs:string"/>
<xs:element name="MA_DDO" minOccurs="0" type="xs:string"/>
<xs:element name="MA_DVIQLY" minOccurs="0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
-<Table1>
<MA_NVGCS/>
<MA_KHANG>PC06LL0192456</MA_KHANG>
<MA_DDO>PC06LL0192456001</MA_DDO>
<MA_DVIQLY>PC06LL</MA_DVIQLY>
</Table1>
I'm using code :
ds.Tables[0].WriteXml(#"C:\Temp\text.xml");
But it just export to format :
-<NewDataSet>
-<Table>
<STATUS>Online</STATUS>
<MESH>false</MESH>
<NGAYGIO>2015-10-05T14:00:27+07:00</NGAYGIO>
<MA_DVIQLY>DSP</MA_DVIQLY>
</Table>
How can I export my XML with xs:schema
Thanks and sorry for my English.
I add XmlWriteMode.WriteSchema to my code ( ds.Tables[0].WriteXml(#"C:\Temp\text.xml", XmlWriteMode.WriteSchema); ) and the problem is solved. Thank to read my stupid question :D
Related
I have a sample to read a xml schema set for a xml file which contains different namespaces. For this i can get different schema for each namespace as i explained below.
Sample File:
<?xml version="1.0" encoding="utf-8"?>
<data xmlns:d="http://sampleschema/dataservices" xmlns:m="http://sampleschema/dataservices/metadata">
<content>
<m:properties>
<d:CustomerID>ALFKI</d:CustomerID>
<d:CompanyName>Alfreds Futterkiste</d:CompanyName>
</m:properties>
</content>
</data>
Sample Code to get XML Schema Set:
strFileName = #"C:\Sample\Sample.xml";
XmlReader reader = XmlReader.Create(strFileName);
XmlSchemaInference schema = new XmlSchemaInference();
XmlSchemaSet schemaSet = schema.InferSchema(reader);
It gives three different types of schemas while using the above code. But my requirement will be i need a single schema for the entire xml file which contain any number of namespaces in it. I have checked with possibilities of codes in msdn and stack overflow. I can't find any proper solution for this.
The expected schema output will be like below.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="content">
<xs:complexType>
<xs:sequence>
<xs:element name="m:properties">
<xs:complexType>
<xs:sequence>
<xs:element name="d:CustomerID" type="xs:string"></xs:element>
<xs:element name="d:CompanyName" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="xmlns:d" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:m" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
Any one can help to achieve this requirement.
Thanks in advance.
Try something like this :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:m="http://www.w3.org/2001/XMLSchema" xmlns:d="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="content">
<m:complexType>
<m:sequence>
<m:element name="properties">
<d:complexType>
<d:sequence>
<d:element name="CustomerID" type="xs:string"></d:element>
<d:element name="CompanyName" type="xs:string"></d:element>
</d:sequence>
</d:complexType>
</m:element>
</m:sequence>
</m:complexType>
</xs:element>
</xs:sequence>
<d:attribute name="xmlns_d" type="d:string"></d:attribute>
<m:attribute name="xmlns_m" type="d:string"></m:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
Consider the following simple schema:
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MyRoot">
<xs:complexType>
<xs:sequence>
<xs:group ref="MyChoice" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:group name="MyChoice">
<xs:choice>
<xs:element name="a" type="xs:string"/>
<xs:element name="b" type="xs:string"/>
<xs:element name="c" type="xs:string"/>
</xs:choice>
</xs:group>
</xs:schema>
When turning this into C# code with xsd.exe (VS2017), I get the following output:
public partial class MyRoot {
private string[] aField;
private string[] bField;
private string[] cField;
// ... omitted for brevity ...
}
This code has a major problem: in the XSD/XML all of the sub-elements (a, b and c) were ordered.
This order is now lost in the C# class, since there are three different arrays for each of the sub elements.
Is xsd.exe doing it wrong? Or am I using it wrong? Or is the .xsd file flawed?
Below is the valid and updated XML I was trying to consume:
<DataSet xmlns="http://tempuri.org/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="response">
<xs:complexType>
<xs:sequence>
<xs:element name="user" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="status" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="code" type="xs:string"/>
<xs:attribute name="value" type="xs:string"/>
<xs:attribute name="description" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="error" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="code" type="xs:string"/>
<xs:attribute name="message" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="operation" type="xs:string"/>
<xs:attribute name="timestamp" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="response"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<response diffgr:id="response1" msdata:rowOrder="0" diffgr:hasChanges="inserted" operation="AUTHENTICATION" timestamp="2015-11-19 18:21:17.457" msdata:hiddenresponse_Id="0">
<user diffgr:id="user1" msdata:rowOrder="0" diffgr:hasChanges="inserted" id="blumaesnetwork" msdata:hiddenresponse_Id="0"/>
<status diffgr:id="status1" msdata:rowOrder="0" diffgr:hasChanges="inserted" code="315" value="FAILED" description="Authentication Failed. User ID Not Found" msdata:hiddenresponse_Id="0"/>
<error diffgr:id="error1" msdata:rowOrder="0" diffgr:hasChanges="inserted" code="-1" message="User Not Found" msdata:hiddenresponse_Id="0"/>
</response>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
With the C# code below:
x.LoadXml(_xmlString);
XDocument x1 = new XDocument();
x1 = XDocument.Parse(_xmlString);
IEnumerable<responseStatus> ListRsts = (from e in x1.Descendants("responseStatus")
select new responseStatus
{
code = e.Element("code").Value,
value = e.Element("value").Value,
description = e.Element("description").Value
});
foreach (var br in ListRsts)
codeField = (br.code);
It keeps throwing error that i missed "diffgr".
What you get as a respsone XML is a saved DataSet. Instead of trying to decipher the diffgram it generates you better leverage the ReadXml method of that type. It is fully equipped to read the xml and gives you specified behavior on the Tables and Relationships.
var ds = new DataSet();
ds.ReadXml(File.Open(#"C:\temp\ds.xml", FileMode.Open));
var listOfStatus = from row in ds.Tables["response"].Rows.Cast<DataRow>()
from status in row.GetChildRows("response_status")
select new { code = status["code"],
value = status["value"],
description = status["description"] };
foreach(var stat in listOfStatus)
{
// do what ever you need to do with the Status record(s)
}
In the above code I read the rows in the table response and then fetch the childrows for the table status. For every status row an anonymous type is created with the three fields you seem to be interested in. You can process those by iterating over the IEnumerable of that anonymous type.
Looking for an easy way to parse a list of strings (or 2d array[string1,string2])
where string 1 is the name and string 2 is the content.
The result should be an .xml containing only the data of string 2 filled in in all the places where the names of the xml schema are the same as string1.
<xs:include schemaLocation="common.xsd"/>
<xs:complexType name="part1Type">
<xs:all>
<xs:element name="noteCadre1" type="string5000Type" minOccurs="0"/>
<xs:element name="noteCadre2" type="string5000Type" minOccurs="0"/>
<xs:element name="noteCadre3" type="string5000Type" minOccurs="0"/>
<xs:element name="noteCadre4" type="string5000Type" minOccurs="0"/>
<xs:element name="noteCadre5" type="string5000Type" minOccurs="0"/>
<xs:element name="noteCadre6" type="string5000Type" minOccurs="0"/>
<xs:element name="noteCadre7" type="string5000Type" minOccurs="0"/>
<xs:element name="noteCadre8" type="string5000Type" minOccurs="0"/>
<xs:element name="noteCadre9" type="string5000Type" minOccurs="0"/>
<xs:element name="noteCadre10" type="string5000Type" minOccurs="0"/>
<xs:element name="noteCadre11" type="string5000Type" minOccurs="0"/>
<xs:element name="noteCadre12" type="string5000Type" minOccurs="0"/>
<xs:element name="infoResidence" type="home2Type" minOccurs="0"/>
<xs:complexType name="homeType">
<xs:sequence>
<xs:element name="lieu" type="string150Type"/>
<xs:element name="dateDebut" type="xs:date" minOccurs="0"/>
<xs:element name="dateFin" type="xs:date" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="home2Type">
<xs:sequence>
<xs:element name="item" type="homeType" maxOccurs="2"/>
</xs:sequence>
</xs:complexType>
The List<String> looks like this:
LIST 1 : {noteCadre1, noteCadre2, .... , infoResidence.lieu.1 , infoResidence.dateDebut.1 ,
infoResidence.dateFin.1 , infoResidence.lieu.2 , infoResidence.dateDebut.2 , ...
LIST 2 : {dataCadre1, dataCadre2, .... , Street 100, 01/01/20005, 25/03/2005, ..... }
Any help would be appreciated; browsed the web for a couple of days without any succes..
You can't do that using only XML Schema: XML Schema is about validating XML documents and not about parsing.
To do this kind of things you need to add a first step that transforms your input document (the list of strings) into your target XML representation.
This seems quite easy to achieve using any programming language (including XSLT 2.0) but this is clearly out of the scope of XML schema languages.
I have an xml document that contains some html.
<begin-line>
<verse-num>6</verse-num>a mixed people<footnote id="f2">
Or <i>a foreign people</i>; Hebrew <i>a bastard</i>
</footnote> shall dwell in Ashdod,
</begin-line>
The verse-num element is the only element I wan't validated, the rest I want valideted to one large group of a string type, which can hold html, and also sometimes some more xml (like footnote).
Here is the schema I have right now which doesn't do the trick.
<xs:element maxOccurs="unbounded" name="begin-line">
<xs:complexType mixed="true">
<xs:sequence minOccurs="0">
<xs:choice maxOccurs="unbounded">
<xs:element name="verse-num">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:unsignedByte">
<xs:attribute name="begin-chapter" type="xs:unsignedByte" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
<xs:attribute name="class" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>
I am using XSD.exe to generate a class that I can deserialize this junk into.
It is generating a begin-line object with a verse-num type below it, and also an array of text, which are split by the html/xml tags inside of .
What I need is an xsd that can be used by XSD.exe to generate a begin-line class that will give me a verse-num type, and then one string property that will contain the rest of the content (including text, i's, b's, footnotes, xml/html).
I did some research and it seems like processContents will do the trick, but I can't figure out where to put it.
When it comes down to it, I want to program against the object created by the XSD.exe like this.
var beginLine = new crosswaybiblePassageVerseunitBeginline();
Console.WriteLine((beginLine.Items[0] as crosswaybiblePassageVerseunitBeginlineVersenum).Value);
Console.Write(beginLine.Text);
or maybe even...
var beginLine = new crosswaybiblePassageVerseunitBeginline();
Console.WriteLine(beginLine.Versenum.Value);
Console.Write(beginLine.Text);
I'm not sure how to setup the schema such that it'll provide nice output from XSD.exe but you can specify "any number of elements with any name" in the output using a type with the definition:
<xs:complexType name="AnyChildren">
<xs:sequence>
<xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded" namespace="##any"/>
</xs:sequence>
<xs:anyAttribute />
</xs:complexType>
For example to validate:
<Module>
<Title>Hello World</Title>
<ProviderType>xyz</ProviderType>
<Content />
<MoreContent />
</Module>
You could use:
<xs:complexType name="Module">
<xs:sequence>
<xs:element name="Title" type="xs:string" maxOccurs="1" />
<xs:element name="ProviderType" type="xs:string" minOccurs="1" nillable="false" />
<xs:any processContents="skip" minOccurs="0" maxOccurs="unbounded" namespace="##any"/>
</xs:sequence>
<xs:anyAttribute />
</xs:complexType>