How to set C# DataTable Locale to be UTC - c#

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!

Related

How to get inline xml schema?

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

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>

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

Categories

Resources