I tried to generate xsd from 2 xml documents. In those 2 xml documents, there are some simple elements (<SubTotal> and <Tax>) and complex type element (<SubTotals>) are optional; that means they may be present or absent from XML documents. Below are 2 sample XML documents and 1 broken xsd document.
I generated xsd document, but it did not work well. By using generated xsd document, when I validated XML documents, the first XML document was valid but the second xml document was not. I want that the xsd should work well with both XML documents. The xml data in those 2 xml documents are valid.
In the first xml document, optional simple type <SubTotal> and <Tax> elements are present, but optional complex type <SubTotals> element is absent.
In the second xml document, optional complex type <SubTotals> element is present, but 2 optional simple type <SubTotal> and <Tax> elements are absent.
The following is the 1st xml document. Is was validated successfully.
<ReceiptMessage>
<DeviceId>AA-BB-CC-DD-EE-FF</DeviceId>
<From>temp#somewhere.com</From>
<To>abc#xyz.com</To>
<Subject>Your Receipt - Version 1</Subject>
<OptIn>255</OptIn>
<Receipt>
<CheckNo>13254</CheckNo>
<TableId>1</TableId>
<ReceiptDate>2015-09-23T11:20:00</ReceiptDate>
<Server>Joy Server</Server>
<CardNo>48757-Loyalty</CardNo>
<PaymentMode>Credit Card</PaymentMode>
<ReceiptHeader>
<string>Some Header 1</string>
<string>Some header 2</string>
</ReceiptHeader>
<SubTotal>35.00</SubTotal>
<Tax>1.00</Tax>
<Total>36.00</Total>
<Gratuity>2.00</Gratuity>
<SplitCheckTotal>38.00</SplitCheckTotal>
<Tip>1.50</Tip>
<AmountPaid>39.50</AmountPaid>
<ReceiptItems>
<ReceiptItem>
<ItemName>Pizza Hut ABC</ItemName>
<Qty>2</Qty>
<Price>4.00</Price>
</ReceiptItem>
<ReceiptItem>
<ItemName>Burito 289</ItemName>
<Qty>1</Qty>
<Price>8.35</Price>
</ReceiptItem>
</ReceiptItems>
<ReceiptFooter>
<string>Thank you for your shopping at our site</string>
<string>Please come back</string>
</ReceiptFooter>
<ReceiptSurvey>
<string>Survey 1 content</string>
<string>Survey - go to our site and register for sweeptakes</string>
</ReceiptSurvey>
</Receipt>
</ReceiptMessage>
The following is the 2nd xml document. Is was not valid.
<ReceiptMessage>
<DeviceId>AA-BB-CC-DD-EE-FF</DeviceId>
<From>temp#somedomain.com</From>
<To>abc#somedomain.com</To>
<Subject>Your Receipt from XYZ- Version 2</Subject>
<OptIn>255</OptIn>
<Receipt>
<CheckNo>17282</CheckNo>
<TableId>Table ABC</TableId>
<ReceiptDate>2015-09-23T16:32:59.4561339-05:00</ReceiptDate>
<Server>John Doe</Server>
<CardNo>2920202</CardNo>
<PaymentMode>Credit Card</PaymentMode>
<ReceiptHeader>
<string>Header 1</string>
<string>Header 2</string>
</ReceiptHeader>
<SubTotals>
<SubtotalItem Label="Subtotal">25.00</SubtotalItem>
<SubtotalItem Label="Sales Tax">3.00</SubtotalItem>
<SubtotalItem Label="City Tax">1.15</SubtotalItem>
<SubtotalItem Label="County Tax">2.25</SubtotalItem>
<SubtotalItem Label="State Tax">1.25</SubtotalItem>
</SubTotals>
<Total>32.65</Total>
<Gratuity>2.00</Gratuity>
<SplitCheckTotal>0.5</SplitCheckTotal>
<Tip>3.00</Tip>
<AmountPaid>38.15</AmountPaid>
<ReceiptItems>
<ReceiptItem>
<ItemName>Pizza</ItemName>
<Qty>1</Qty>
<Price>5.32</Price>
</ReceiptItem>
<ReceiptItem>
<ItemName>Burito</ItemName>
<Qty>2</Qty>
<Price>10.99</Price>
</ReceiptItem>
</ReceiptItems>
<ReceiptFooter>
<string>Footer 1</string>
<string>Footer 2</string>
</ReceiptFooter>
<ReceiptSurvey>
<string>Go to our site to register and win awards to</string>
</ReceiptSurvey>
</Receipt>
</ReceiptMessage>
I would like to have a correct version for the following generated xsd. It should work with those 2 XML documents.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ReceiptMessage" nillable="true" type="ReceiptMessage" />
<xs:complexType name="ReceiptMessage">
<xs:complexContent mixed="false">
<xs:extension base="EmailParams">
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="OptIn" type="xs:unsignedByte" />
<xs:element minOccurs="0" maxOccurs="1" name="Receipt" type="ReceiptData" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="EmailParams">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="DeviceId" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="From" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="To" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="Subject" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ReceiptData">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="CheckNo" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="TableId" type="xs:string" />
<xs:element minOccurs="1" maxOccurs="1" name="ReceiptDate" type="xs:dateTime" />
<xs:element minOccurs="0" maxOccurs="1" name="Server" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="CardNo" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="PaymentMode" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="ReceiptHeader" type="ArrayOfString" />
<xs:element minOccurs="1" maxOccurs="1" name="SubTotal" nillable="true" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="Tax" nillable="true" type="xs:double" />
<xs:element minOccurs="0" maxOccurs="1" name="SubTotals" type="ArrayOfSubtotalItem" />
<xs:element minOccurs="1" maxOccurs="1" name="Total" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="Gratuity" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="SplitCheckTotal" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="Tip" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="AmountPaid" type="xs:double" />
<xs:element minOccurs="0" maxOccurs="1" name="ReceiptItems" type="ArrayOfReceiptItem" />
<xs:element minOccurs="0" maxOccurs="1" name="ReceiptFooter" type="ArrayOfString" />
<xs:element minOccurs="0" maxOccurs="1" name="ReceiptSurvey" type="ArrayOfString" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ArrayOfString">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ArrayOfSubtotalItem">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="SubtotalItem" nillable="true" type="SubtotalItem" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="SubtotalItem">
<xs:simpleContent>
<xs:extension base="xs:double">
<xs:attribute name="Label" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="ArrayOfReceiptItem">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="ReceiptItem" nillable="true" type="ReceiptItem" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ReceiptItem">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="ItemName" type="xs:string" />
<xs:element minOccurs="1" maxOccurs="1" name="Qty" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="Price" type="xs:double" />
</xs:sequence>
</xs:complexType>
</xs:schema>
I use the following free online tool to validate my XML documents based on generated XSD:
http://www.freeformatter.com/xml-validator-xsd.html
Your code has:
<xs:element minOccurs="1" maxOccurs="1" name="SubTotal" nillable="true" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="Tax" nillable="true" type="xs:double" />
And you wrote that sometimes Tax or SubTotal can be absent. In other words, the minOccurs should be set to 0.
Typically, when you auto-generate an XSD from an instance XML document, there will be gaps that the XSD generator will not be able to assess, simply because information in an instance XML document will never be complete.
At best, such auto-generation gets you going, but you'll always have to do some tweaks afterwards. Use a good (graphical) XSD designer (oXygen, Visual Studio, Eclipse, LiquidXML etc) which lets you drag/drop and set properties through a convenient interface.
Related
I have an XML file
perhaps i usually do more than 100 query every minute with C# and XMLDatareader, that's why i am thinking to add an Index on it, is it possible? or Indexes are allowed only on DB like sqlserver/mysql?
thanks
Here my XML file, with the first record:
<?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:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Traduzioni">
<xs:complexType>
<xs:sequence>
<xs:element name="Path" type="xs:string" />
<xs:element name="IT" type="xs:string" minOccurs="0" />
<xs:element name="EN" type="xs:string" minOccurs="0" />
<xs:element name="FR" type="xs:string" minOccurs="0" />
<xs:element name="PT" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="extension_ref">
<xs:complexType>
<xs:sequence>
<xs:element name="idKey" type="xs:string" minOccurs="0" />
<xs:element name="Reference" type="xs:string" minOccurs="0" />
<xs:element name="Ordine" type="xs:string" minOccurs="0" />
<xs:element name="Dizione_EN" type="xs:string" minOccurs="0" />
<xs:element name="Dizione_IT" type="xs:string" minOccurs="0" />
<xs:element name="Dizione_PT" type="xs:string" minOccurs="0" />
<xs:element name="Dizione_FR" type="xs:string" minOccurs="0" />
<xs:element name="Dizione_SP" type="xs:string" minOccurs="0" />
<xs:element name="idKey_old" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//Traduzioni" />
<xs:field xpath="Path" />
</xs:unique>
</xs:element>
</xs:schema>
<Traduzioni>
<Path>N.Ordine</Path>
<IT>N.Ordine</IT>
<EN>Order number</EN>
<FR>Numéro de commande</FR>
<PT>No de ordem:</PT>
</Traduzioni>
That looks like a translation file of some sort.
You'd be best off loading the XML into memory, e.g. a Dictionary<string, Dictionary<string, string>>, keyed by target language, then by original string. (This is rather trivial to do, so I won't post the code here.)
That way you can then access translations with
translations["EN"]["N.Ordine"]
I'm looking to make a Schema for an XML report that has no restrictions at all on the order in which elements can appear, but has an element that must appear once, elements that can appear once, and elements that can appear any number of times.
I created an XSD following the second answer to this question, as that solution (albeit ugly) should solve my problem. However, using Microsoft's xsd.exe tool to generate classes for the xsd or using XmlDocument.Validate() gave the following warning:
Schema validation warning: Multiple definition of element 'optional2' causes the content model to become ambiguous. A content model must be formed such that...
The error is at line 21, which is the second time 'optional2' appears in the xsd.
Below is what I had for my version of the second answer to the question I referenced earlier.
<xs:group name="unboundedElements">
<xs:choice>
<xs:element name="unbounded1" type="unbounded1Type"/>
<xs:element name="unbounded2" type="unbounded2Type"/>
<xs:element name="unbounded3" type="unbounded3Type"/>
<xs:element name="unbounded4" type="unbounded4Type"/>
</xs:choice>
</xs:group>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:choice>
<xs:sequence>
<xs:element name="optional1" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="optional2" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="optional3" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="optional4" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="required" type="queryType" maxOccurs="1"/>
</xs:sequence>
<xs:sequence>
<xs:element name="optional2" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="optional1" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="optional3" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="optional4" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="required" type="queryType" maxOccurs="1"/>
</xs:sequence>
<xs:sequence>
<xs:element name="optional3" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="optional2" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="optional1" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="optional4" type="xs:string" maxOccurs="1" minOccurs="0"/>
<xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="required" type="queryType" maxOccurs="1"/>
// And so on, since this is 5! = 120 permutations
</xs:sequence>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
I'm hoping that someone could point out and explain what I'm doing wrong.
I could "validate" the xml by just using a <xs:choice maxOccurs="unbounded"> tag around all the elements, as that would allow for the order to not matter, but it would not place any of the necessary restrictions on the xml document.
Also, I know xsd 1.1 allows using maxOccurs="unbounded" with <xs:all> which would solve the problem perfectly, but .NET doesn't support using xsd 1.1.
I'm thinking my best solution going forward would probably be to just sort of enforce strict ordering of elements by sorting the xml nodes before validating it (allowing the xsd document to be far simpler and correct), but I still want to learn more about xsd and try to figure out what's wrong with my attempt.
If you want to have the nodes "optional1" ... "optional4" only once in a specific section (not several times and unsorted), then you have to define all possible orders with the first node mandatory in order to get rid of the ambiguity. You may have to follow this method several times. Hope this helps, Peter
I am using C# 4.5/Visual Studio 2012 to try to load an XML file to a DataSet. I read the instructions here:
http://msdn.microsoft.com/en-us/library/fx29c3yd.aspx
I am loading an XML file that looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<UnitTestDataSet xmlns="https://rebuildinghomes.codeplex.com/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://rebuildinghomes.codeplex.com/ UnitTestLoadData.xsd">
<Person>
<PersonId>1111</PersonId>
<FirstName>Unit</FirstName>
<LastName>Tester</LastName>
</Person>
</UnitTestDataSet>
With a schema like this:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="RebuildingUnitTestData"
targetNamespace="https://rebuildinghomes.codeplex.com/"
xmlns:u="https://rebuildinghomes.codeplex.com/"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="PersonId" type="xs:int"
minOccurs="1" maxOccurs="1" />
<xs:element name="FirstName" type="xs:string"
minOccurs="1" maxOccurs="1" />
<xs:element name="LastName" type="xs:string"
minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Using the following C# code:
//Load init/expected
DataSet init = new DataSet();
init.ReadXmlSchema("RebuildingModel/Data/UnitTestLoadData.xsd");
init.ReadXml("RebuildingModel/Data/PersonUnitTest.xml", XmlReadMode.ReadSchema);
However, when I look at "init" in the debugger, the DataSet is empty. What am I doing wrong?
Sadly, your snippet of code seem to work on my machine. Are you sure you're checking init AFTER the init.ReadXml() statement executes?
See my output from my Visual Studio immediate window below:
ds.Tables[0]
{Person}
base {System.ComponentModel.MarshalByValueComponent}: {Person}
CaseSensitive: false
ChildRelations: {System.Data.DataRelationCollection.DataTableRelationCollection}
Columns: {System.Data.DataColumnCollection}
Constraints: {System.Data.ConstraintCollection}
DataSet: {System.Data.DataSet}
DefaultView: {System.Data.DataView}
DisplayExpression: ""
ExtendedProperties: Count = 0
HasErrors: false
IsInitialized: true
Locale: {en-US}
MinimumCapacity: 50
Namespace: "https://rebuildinghomes.codeplex.com/"
ParentRelations: {System.Data.DataRelationCollection.DataTableRelationCollection}
Prefix: ""
PrimaryKey: {System.Data.DataColumn[0]}
RemotingFormat: Xml
Rows: {System.Data.DataRowCollection}
Site: null
TableName: "Person"
ds.Tables[0].Rows
{System.Data.DataRowCollection}
base {System.Data.InternalDataCollectionBase}: {System.Data.DataRowCollection}
Count: 1
My DataSet is populated with a single table containing a single row as per your XML.
I finally figured this out.
The schema now looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="UnitTestDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
<xs:annotation>
<xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
<DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
<Connections />
<Tables />
<Sources />
</DataSource>
</xs:appinfo>
</xs:annotation>
<xs:element name="UnitTestDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_DataSetName="UnitTestDataSet" msprop:Generator_UserDSName="UnitTestDataSet">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Person" msprop:Generator_TableClassName="PersonDataTable" msprop:Generator_TableVarName="tablePerson" msprop:Generator_TablePropName="Person" msprop:Generator_RowDeletingName="PersonRowDeleting" msprop:Generator_RowChangingName="PersonRowChanging" msprop:Generator_RowEvHandlerName="PersonRowChangeEventHandler" msprop:Generator_RowDeletedName="PersonRowDeleted" msprop:Generator_UserTableName="Person" msprop:Generator_RowChangedName="PersonRowChanged" msprop:Generator_RowEvArgName="PersonRowChangeEvent" msprop:Generator_RowClassName="PersonRow">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonId" msprop:Generator_ColumnVarNameInTable="columnPersonId" msprop:Generator_ColumnPropNameInRow="PersonId" msprop:Generator_ColumnPropNameInTable="PersonIdColumn" msprop:Generator_UserColumnName="PersonId" type="xs:int" />
<xs:element name="FirstName" msprop:Generator_ColumnVarNameInTable="columnFirstName" msprop:Generator_ColumnPropNameInRow="FirstName" msprop:Generator_ColumnPropNameInTable="FirstNameColumn" msprop:Generator_UserColumnName="FirstName" type="xs:string" />
<xs:element name="LastName" msprop:Generator_ColumnVarNameInTable="columnLastName" msprop:Generator_ColumnPropNameInRow="LastName" msprop:Generator_ColumnPropNameInTable="LastNameColumn" msprop:Generator_UserColumnName="LastName" type="xs:string" />
<xs:element name="HomePhoneNbr" msprop:Generator_ColumnVarNameInTable="columnHomePhoneNbr" msprop:Generator_ColumnPropNameInRow="HomePhoneNbr" msprop:Generator_ColumnPropNameInTable="HomePhoneNbrColumn" msprop:Generator_UserColumnName="HomePhoneNbr" type="xs:string" minOccurs="0" />
<xs:element name="CellPhoneNbr" msprop:Generator_ColumnVarNameInTable="columnCellPhoneNbr" msprop:Generator_ColumnPropNameInRow="CellPhoneNbr" msprop:Generator_ColumnPropNameInTable="CellPhoneNbrColumn" msprop:Generator_UserColumnName="CellPhoneNbr" type="xs:string" minOccurs="0" />
<xs:element name="StreetAddress" msprop:Generator_ColumnVarNameInTable="columnStreetAddress" msprop:Generator_ColumnPropNameInRow="StreetAddress" msprop:Generator_ColumnPropNameInTable="StreetAddressColumn" msprop:Generator_UserColumnName="StreetAddress" type="xs:string" minOccurs="0" />
<xs:element name="AptNbr" msprop:Generator_ColumnVarNameInTable="columnAptNbr" msprop:Generator_ColumnPropNameInRow="AptNbr" msprop:Generator_ColumnPropNameInTable="AptNbrColumn" msprop:Generator_UserColumnName="AptNbr" type="xs:string" minOccurs="0" />
<xs:element name="City" msprop:Generator_ColumnVarNameInTable="columnCity" msprop:Generator_ColumnPropNameInRow="City" msprop:Generator_ColumnPropNameInTable="CityColumn" msprop:Generator_UserColumnName="City" type="xs:string" minOccurs="0" />
<xs:element name="County" msprop:Generator_ColumnVarNameInTable="columnCounty" msprop:Generator_ColumnPropNameInRow="County" msprop:Generator_ColumnPropNameInTable="CountyColumn" msprop:Generator_UserColumnName="County" type="xs:string" minOccurs="0" />
<xs:element name="StateProvCd" msprop:Generator_ColumnVarNameInTable="columnStateProvCd" msprop:Generator_ColumnPropNameInRow="StateProvCd" msprop:Generator_ColumnPropNameInTable="StateProvCdColumn" msprop:Generator_UserColumnName="StateProvCd" type="xs:string" minOccurs="0" />
<xs:element name="PostalCode" msprop:Generator_ColumnVarNameInTable="columnPostalCode" msprop:Generator_ColumnPropNameInRow="PostalCode" msprop:Generator_ColumnPropNameInTable="PostalCodeColumn" msprop:Generator_UserColumnName="PostalCode" type="xs:string" minOccurs="0" />
<xs:element name="CountryCode" msprop:Generator_ColumnVarNameInTable="columnCountryCode" msprop:Generator_ColumnPropNameInRow="CountryCode" msprop:Generator_ColumnPropNameInTable="CountryCodeColumn" msprop:Generator_UserColumnName="CountryCode" type="xs:string" minOccurs="0" />
<xs:element name="NumberOfYearsAtAddr" msprop:Generator_ColumnVarNameInTable="columnNumberOfYearsAtAddr" msprop:Generator_ColumnPropNameInRow="NumberOfYearsAtAddr" msprop:Generator_ColumnPropNameInTable="NumberOfYearsAtAddrColumn" msprop:Generator_UserColumnName="NumberOfYearsAtAddr" type="xs:int" minOccurs="0" />
<xs:element name="EmailAddress" msprop:Generator_ColumnVarNameInTable="columnEmailAddress" msprop:Generator_ColumnPropNameInRow="EmailAddress" msprop:Generator_ColumnPropNameInTable="EmailAddressColumn" msprop:Generator_UserColumnName="EmailAddress" type="xs:string" minOccurs="0" />
<xs:element name="GenderCode" msprop:Generator_ColumnVarNameInTable="columnGenderCode" msprop:Generator_ColumnPropNameInRow="GenderCode" msprop:Generator_ColumnPropNameInTable="GenderCodeColumn" msprop:Generator_UserColumnName="GenderCode" type="xs:string" minOccurs="0" />
<xs:element name="EthnicityCode" msprop:Generator_ColumnVarNameInTable="columnEthnicityCode" msprop:Generator_ColumnPropNameInRow="EthnicityCode" msprop:Generator_ColumnPropNameInTable="EthnicityCodeColumn" msprop:Generator_UserColumnName="EthnicityCode" type="xs:int" minOccurs="0" />
<xs:element name="BirthDate" msprop:Generator_ColumnVarNameInTable="columnBirthDate" msprop:Generator_ColumnPropNameInRow="BirthDate" msprop:Generator_ColumnPropNameInTable="BirthDateColumn" msprop:Generator_UserColumnName="BirthDate" type="xs:date" minOccurs="0" />
<xs:element name="ResidentStatus" msprop:Generator_ColumnVarNameInTable="columnResidentStatus" msprop:Generator_ColumnPropNameInRow="ResidentStatus" msprop:Generator_ColumnPropNameInTable="ResidentStatusColumn" msprop:Generator_UserColumnName="ResidentStatus" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//Person" />
<xs:field xpath="PersonId" />
</xs:unique>
</xs:element>
</xs:schema>
The big thing that changed, as you'll notice, is all of the extraneous junk that Visual Studio adds in. Apparently, .NET needs some of this to load the data properly, most notably the primary key annotation. So the moral of the story: if you're going to load a DataSet from XML, create a schema through Visual Studio or be prepared to run it through XSLT to add all this stuff, or the results will be unpredictable, at best.
Could it be the space in the following:
xsi:schemaLocation="https://rebuildinghomes.codeplex.com/ UnitTestLoadData.xsd">
from your xml file?
Several time already asked here and there, some answers relate to old VS versions (this on is using V.S. 2012).
I present the problem again:
given an xsd:
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="LocationType">
<xs:attribute name="X" type="xs:integer" />
<xs:attribute name="Y" type="xs:integer" />
</xs:complexType>
<xs:complexType name="AlphaNumericType">
<xs:sequence>
<xs:element name="AlphaNumericLocation" type="LocationType" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" />
<xs:attribute name="key" type="xs:integer" />
</xs:complexType>
<xs:complexType name="BitmapType">
<xs:sequence>
<xs:element name="BitmapLocation" type="LocationType" />
<xs:element name="BitmapCaptions" type="AlphaNumericType" />
</xs:sequence>
<xs:attribute name="key" type="xs:string" />
<xs:attribute name="id" type="xs:string" />
</xs:complexType>
<xs:complexType name="ArcType">
<xs:sequence>
<xs:element name="ArcLocation" type="LocationType" />
<xs:element name="ArcCaptions" type="AlphaNumericType" />
</xs:sequence>
<xs:attribute name="key" type="xs:string" />
<xs:attribute name="id" type="xs:string" />
</xs:complexType>
<xs:element name="BitmapControls">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="Bitmap" type="BitmapType" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ArcControls">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="Arc" type="ArcType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Notice - that AlphaNumeric has a location element, and both bitmap and arc has AlphaNumeric.
When I create a cs class (using XSD tool) and try to instantiate it I get this error:
The same table 'AlphaNumericLocation' cannot be the child table in two
nested relations.
How can I overcome this issue ?
(the real xsd is more complicated and has a lot more "related similar" children.....
I want to use the xml data in my app in a typed dataset (which easily read and parse the xml).
and I can easily bind tables and columns to other controls... (grid)
Microsoft's XML parser does not support this: I don't think it's changed since early versions of VS.
Check here for some hints on what to do instead: http://social.msdn.microsoft.com/Forums/en-US/22f98352-83b9-4638-a306-34a36a11e4d6/the-same-table-choice-cannot-be-the-child-table-in-two-nested-relations
I have posted a workaround for this problem.
Obviously it may not be a solution for every one,
but at least it explains the source of the problem an points a finger to the offending code.
Serialization Issue when using WriteXML method
Also, you should use 'nesting' in your schema, that will fix the problem.
I have problems adapting your schema, so you'll have to try yourself, but I have adapted one of my own. It's a DataSet with two tables, where MyRootTable has a nested relation with 'PremiumPerYear'.
The key player in this nested relation is the <xs:choice element.
It allows (I believe) the schema to reference another part of itself.
This reference is then created/used by the 'ref' keyword:
<xs:element ref="PremiumPerYear" />
Note: this example does not have an actual 'double nested' relation, but that's just because I cut 90kb of text out.
<DataSet>
<xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
<xs:element name="PremiumPerYear">
<xs:complexType>
<xs:sequence>
<xs:element name="BeforeTaxes" type="xs:decimal" minOccurs="0" />
<xs:element name="AfterTaxes" type="xs:decimal" minOccurs="0" />
</xs:sequence>
</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="PremiumPerYear" />
<xs:element name="MyRootTable">
<xs:complexType>
<xs:sequence>
<xs:element name="RequestType" msprop:KeyValueCategory="KindOfRequest" type="xs:string" minOccurs="0" />
<xs:element name="RequestDateTime" type="xs:dateTime" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
</DataSet>
I auto generated an xsd file from the below xml and used xsd2code to get a c# class. The problem is the entire xml doesn't deserialize.
Here is how I'm attempting to deserialize:
static void Main(string[] args)
{
using (TextReader textReader = new StreamReader("config.xml"))
{
// string temp = textReader.ReadToEnd();
XmlSerializer deserializer = new XmlSerializer(typeof(project));
project p = (project)deserializer.Deserialize(textReader);
}
}
here is the actual XML:
<?xml version='1.0' encoding='UTF-8'?>
<project>
<scm class="hudson.scm.SubversionSCM">
<locations>
<hudson.scm.SubversionSCM_-ModuleLocation>
<remote>https://svn.xxx.com/test/Validation/CPS DRTest DLL/trunk</remote>
</hudson.scm.SubversionSCM_-ModuleLocation>
</locations>
<useUpdate>false</useUpdate>
<browser class="hudson.scm.browsers.FishEyeSVN">
<url>http://fisheye.xxxx.net/browse/Test/</url>
<rootModule>Test</rootModule>
</browser>
<excludedCommitMessages></excludedCommitMessages>
</scm>
<openf>Hello there</openf>
<buildWrappers/>
</project>
When I run the above, the locations node remains null.
Here is the xsd that I'm using:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="project">
<xs:complexType>
<xs:all>
<xs:element name="openf" type="xs:string" minOccurs="0" />
<xs:element name="buildWrappers" type="xs:string" minOccurs="0" />
<xs:element name="scm" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="useUpdate" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
<xs:element name="excludedCommitMessages" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
<xs:element name="locations" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="hudson.scm.SubversionSCM_-ModuleLocation" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="remote" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="browser" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="url" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
<xs:element name="rootModule" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
</xs:sequence>
<xs:attribute name="class" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="class" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:all>
</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="project" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Figured it out. When using xsd2code I had select options to target the 3.5 framework and include the xml attributes. It now deserializes as expected. Not sure which one did it, but works now.
According to this page '.' is not a valid XML tag name character, so you need to rename <hudson.scm.SubversionSCM_-ModuleLocation> to something without the dots.