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

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

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!

How to define the unique value in xml file [duplicate]

This question already has an answer here:
I want unique value for telephone element
(1 answer)
Closed 5 years ago.
I want to unique key in mobile number when two same mobile number found in xml file.
My XML File :
<EmployeeDetails>
<Employee>
<Name>ABC</Name>
<MobileNumber>9876543210</MobileNumber>
</Employee>
<Employee>
<Name>XYZ</Name>
<MobileNumber>9876543210</MobileNumber>
</Employee>
</EmployeeDetails>
My XSD File :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="EmployeeDetails">
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string"></xs:element>
<xs:element name="MobileNumber" type="xs:int"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
When run the code if duplicate mobile number found then display error massage
Change your xsd with this code
<xs:element name="Name"/>
<xs:element name="MobileNumber" type="xs:long"/>
<xs:complexType>
<xs:sequence>
<xs:element ref="Employee"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="Any name you want to give">
<xs:selector xpath="Employee"/>
<xs:field xpath="MobileNumber"/>
</xs:unique>

XDocument.Validate returning The Element has invalid child element

Getting a really strange issue today while trying to validate XML using XSD.
Looking at the XML I'm providing, it looks correct.
The error I'm receiving from XDocument.Validate is:
The element 'APPOINTMENTS' had invalid child element 'APPOINTMENT'
Here is the XML I'm using:
<PATIENTS>
<PATIENT>
<APPOINTMENTS>
<APPOINTMENT>
<UserInitials>123</UserInitials>
<Date>Some Date</Date>
<ApptTime>14:30</ApptTime>
<Duration>00:15</Duration>
<AppointmentStatus>Complete</AppointmentStatus>
<Notes>Some note</Notes>
<TreatmentType>Some Appoinment type</TreatmentType>
</APPOINTMENT>
</APPOINTMENTS>
</PATIENT>
</PATIENTS>
And the XSD file I'm validating against:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="PATIENTS">
<xs:complexType>
<xs:sequence>
<xs:element name="PATIENT" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="APPOINTMENTS" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="APPOINTMENT" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="UserInitials" type="xs:string" minOccurs="1"></xs:element>
<xs:element name="Date" type="xs:string" minOccurs="1"></xs:element>
<xs:element name="ApptTime" type="xs:string" minOccurs="1"></xs:element>
<xs:element name="Duration" type="xs:string" minOccurs="1"></xs:element>
<xs:element name="AppointmentStatus" type="xs:string" minOccurs="1"></xs:element>
<xs:element name="LegacyTypeID" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="AClinic" minOccurs="0"></xs:element>
<xs:element name="Notes" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="Info" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="TreatmentType" type="xs:string" minOccurs="0" default="Examination"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I don't quite understand what is happening, it looks like the Appointments and Appointments tags are conforming to the XSD file.
The rest of the XML document looks correct, unless there is an issue with the XSD file.
I do have other Elements within my Patient Element that are working fine.
I've worked out the problem, I was actually missing part of the XML to show you.
I should have included the following:
<?xml version="1.0" encoding="utf-8" ?>
<PATIENTS>
<PATIENT>
<APPOINTMENTS>
<APPOINTMENT>
<UserInitials>123</UserInitials>
<Date>Some Date</Date>
<ApptTime>14:30</ApptTime>
<Duration>00:15</Duration>
<AppointmentStatus>Complete</AppointmentStatus>
<Notes>Some note</Notes>
<TreatmentType>Some Appoinment type</TreatmentType>
</APPOINTMENT>
<APPOINTMENT>
<UserInitials>123</UserInitials>
<Date>Some Date</Date>
<ApptTime>14:30</ApptTime>
<Duration>00:15</Duration>
<AppointmentStatus>Complete</AppointmentStatus>
<Notes>Some note</Notes>
<TreatmentType>Some Appoinment type</TreatmentType>
</APPOINTMENT>
<APPOINTMENT>
<UserInitials>123</UserInitials>
<Date>Some Date</Date>
<ApptTime>14:30</ApptTime>
<Duration>00:15</Duration>
<AppointmentStatus>Complete</AppointmentStatus>
<Notes>Some note</Notes>
<TreatmentType>Some Appoinment type</TreatmentType>
</APPOINTMENT>
</APPOINTMENTS>
</PATIENT>
</PATIENTS>
I Actually have multiple records within the APPOINTMENT Element, which would require the XSD file to have the following on the APPOINTMENT element:
<xs:element name="APPOINTMENT" minOccurs="0" maxOccurs="unbounded">
I was missing the maxOccurs="unbounded" attribute

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