I am trying to add an xsd into an XmlSchemaSet but get the following error "Data at the root level is invalid. Line 1, position 1."
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(#"C:\source\Net4\Clocks\Handlers\XML_Schemas\test.xsd")));
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="interface">
<xs:complexType>
<xs:sequence>
<xs:element name="node1" type="xs:string" />
<xs:element name="node2" type="xs:string" />
<xs:element name="node3" type="xs:string" />
<xs:element name="node4" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="node5" type="xs:unsignedByte" />
<xs:element name="node6">
<xs:complexType>
<xs:sequence>
<xs:element name="node6" type="xs:dateTime" />
<xs:element name="node7" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
You need to pass contents of a file into StringReader, not the path to a file.
var pathToFile = #"C:\source\Net4\Clocks\Handlers\XML_Schemas\test.xsd";
var reader = new StringReader(File.ReadAllText(pathToFile));
schemas.Add("", XmlReader.Create(reader));
Alternativ way to add reader to XmlSchemaSet is
schemas.Add("", new XmlTextReader(pathToFile));
Related
I have the following xsd file...
<?xml version="1.0" encoding="Windows-1252"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="ordername" maxOccurs="1" minOccurs="1" type="xs:string" />
<xs:element name="articles">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="unbounded" name="article">
<xs:complexType>
<xs:sequence>
<xs:element name="Account" maxOccurs="1" minOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="1|5|7|9"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Vat" maxOccurs="1" minOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="1|2|3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I am trying to access all allowed patterns for a specific element. In this case I would like to find the allowed pattern for "Account" which should give me "1|5|7|9". Ideally I would like to be able to directly access the value, something like this...
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
var nodeToAccess = xmlDoc.DocumentElement.SelectSingleNode("order/articles/article/Account", nsmgr);
But that does not work, can anyone please show the proper way to do this?
You can use XPath for this:
var nodeToAccess = xmlDoc.SelectSingleNode("//xs:element[#name='Account']/xs:simpleType/xs:restriction/xs:pattern", nsmgr);
if (nodeToAccess != null)
{
Console.WriteLine(nodeToAccess.Attributes["value"].Value);
}
I have the below xsd schema and I want to add the DG collection inside DGItems. So I want to know how can I achieve that.
When look at my xml at the 2nd section in that manner I want to achieve that. Just a little help needed to fix that.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="InvoiceData" type="InvoiceData"/>
<xs:complexType name="InvoiceData">
<xs:sequence>
<xs:element name="HeaderFields" type="HeaderFields"/>
<xs:element name="DGItems" type="DGItems"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="HeaderFields">
<xs:sequence>
<xs:element name="CompanyId" type="xs:string" />
<xs:element name="ImageID" type="xs:string" />
<xs:element name="Incident" type="xs:string" />
<xs:element name="FacilityID" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="DGItems">
<xs:sequence>
<xs:element name="DG1" type="xs:string" />
<xs:element name="DG2" type="xs:string" />
<xs:element name="DG3" type="xs:string" />
<xs:element name="DG4" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
<InvoiceValues>
<HeaderFields>
<CompanyId>StringValue</CompanyId>
<ImageID>StringValue</ImageID>
<Incident>StringValue</Incident>
<FacilityID>StringValue</FacilityID>
</HeaderFields>
<DGItems>
<Dg>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
</Dg>
<Dg>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
</Dg>
</DGItems>
</InvoiceValues>
Need your help on this.
I have solved the issue on my own, the thing which i have done to fix my issue is are as follows : I have added the Element AI inside the Sequence node & in this way I was managed to fix my issue.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="EmployeeData" type="EmployeeData"/>
<xs:complexType name="EmployeeData">
<xs:sequence>
<xs:element name="EmployeeHeaderData" type="EmployeeHeaderData"/>
<xs:element name="AddressItems" type="AddressItems"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="EmployeeHeaderData">
<xs:sequence>
<xs:element name="CompanyId" type="xs:string" />
<xs:element name="ImageID" type="xs:string" />
<xs:element name="Incident" type="xs:string" />
<xs:element name="FacilityID" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="AddressItems">
<xs:sequence>
<xs:element name="AI" minOccurs="0" maxOccurs="unbounded" >
<xs:complexType>
<xs:sequence>
<xs:element name="AI1" type="xs:string" />
<xs:element name="AI2" type="xs:string" />
<xs:element name="AI3" type="xs:string" />
<xs:element name="AI4" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
I have the following XML file like this:
<?xml version="1.0" encoding="utf-8"?>
<Config version="Vs1.00.00"><!-- xmlns="urn:xmldata-schema">-->
<<SystemSettings>
<Workstation></Workstation>
<Company></Company>
<InstallationDate></InstallationDate>
<SupportContactLine1></SupportContactLine1>
<SupportContactLine2></SupportContactLine2>
<SupportContactLine3></SupportContactLine3>
<UserFile></UserFile>
</SystemSettings>
<Settings>
<StartUpWorkspace></StartUpWorkspace>
<StartLanguage></StartLanguage>
<ExportPath></ExportPath>
<ImportPath></ImportPath>
<BackUpRestorePath></BackUpRestorePath>
<AdjHistoryPath></AdjHistoryPath>
<TestHistoryPath></TestHistoryPath>
<LogFilePath></LogFilePath>
</Settings>
<DefaultSettings>
<DefaultSupportLine1></DefaultSupportLine1>
<DefaultSupportLine2></DefaultSupportLine2>
<DefaultSupportLine3></DefaultSupportLine3>
<DefaultUserFile></DefaultUserFile>
<DefaultStartUpWorkspace></DefaultStartUpWorkspace>
<DefaultStartLanguage></DefaultStartLanguage>
<DefaultWorkspacePath></DefaultWorkspacePath>
<DefaultExportPath></DefaultExportPath>
<DefaultImportPath></DefaultImportPath>
<DefaultBackUpRestorePath></DefaultBackUpRestorePath>
<DefaultAdjHistoryPath></DefaultAdjHistoryPath>
<DefaultTestHistoryPath></DefaultTestHistoryPath>
<DefaultLogFilePath></DefaultLogFilePath>
<DefaultMasterPassword crypt="No"></DefaultMasterPassword>
<DefaultLogger></DefaultLogger>
</DefaultSettings>
</Config>
I need to validate another XML file. Is there a way I can check another XML file if it has the same structure as my file here?
You need an XSD file to check whether an xml is proper. If you do not have the xsd file, some tools may create xsd from xml, but you may need to modify created xsd.
This answer describes how to validate an xml file in c# against an XSD file.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Config">
<xs:complexType>
<xs:sequence>
<xs:element name="SystemSettings">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="Workstation"/>
<xs:element type="xs:string" name="Company"/>
<xs:element type="xs:string" name="InstallationDate"/>
<xs:element type="xs:string" name="SupportContactLine1"/>
<xs:element type="xs:string" name="SupportContactLine2"/>
<xs:element type="xs:string" name="SupportContactLine3"/>
<xs:element type="xs:string" name="UserFile"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Settings">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="StartUpWorkspace"/>
<xs:element type="xs:string" name="StartLanguage"/>
<xs:element type="xs:string" name="ExportPath"/>
<xs:element type="xs:string" name="ImportPath"/>
<xs:element type="xs:string" name="BackUpRestorePath"/>
<xs:element type="xs:string" name="AdjHistoryPath"/>
<xs:element type="xs:string" name="TestHistoryPath"/>
<xs:element type="xs:string" name="LogFilePath"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DefaultSettings">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="DefaultSupportLine1"/>
<xs:element type="xs:string" name="DefaultSupportLine2"/>
<xs:element type="xs:string" name="DefaultSupportLine3"/>
<xs:element type="xs:string" name="DefaultUserFile"/>
<xs:element type="xs:string" name="DefaultStartUpWorkspace"/>
<xs:element type="xs:string" name="DefaultStartLanguage"/>
<xs:element type="xs:string" name="DefaultWorkspacePath"/>
<xs:element type="xs:string" name="DefaultExportPath"/>
<xs:element type="xs:string" name="DefaultImportPath"/>
<xs:element type="xs:string" name="DefaultBackUpRestorePath"/>
<xs:element type="xs:string" name="DefaultAdjHistoryPath"/>
<xs:element type="xs:string" name="DefaultTestHistoryPath"/>
<xs:element type="xs:string" name="DefaultLogFilePath"/>
<xs:element name="DefaultMasterPassword">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="crypt"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="DefaultLogger"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
You need to create xml scheme and then use XDocument.Validate method to check if the xml conform the scheme
You can create a xsd schema that possibly can look something like this. (I recommend #daryal's xml schema , but i would throw some constraints on the xml elements.)
<xs:element name="SystemSettings">
<xs:complexType>
<xs:sequence>
<xs:element name="Workstation" minOccurs="0" maxOccurs="1" />
...
</xs:sequence>
</xs:complexType>
...
Then you can use this code to perform validation
using (var xsdStream = new MemoryStream(fXmlSchema))
{
xsdStream .Position = 0;
using (var xsdReader = XmlReader.Create(xsdStream ))
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.Schemas.Add(XmlSchema.Read(xsdReader , [performingValidationCallback]));
using (var xmlStream = new MemoryStream(fXml))
{
using (var xmlReader = XmlReader.Create(xmlStream, settings ))
{
try
{
xmlStream .Position = 0;
while (xmlReader .Read())
{
}
}
catch (XmlException e)
{
//VALIDATIONERROR
}
}
}
}
}
I have the following XML message that I deserialize with XmlSerializer.
For some reasons, deserialization of this message strips the Database section of the XML message.
I need some help in deserializing this message in its entirety.
This is a sample test XML message.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Radars xmlns="http://mycompany.com/2008/c2s2">
<Radar>
<Identification>
<Identifier>140:141</Identifier>
<IP>10.216.1.79</IP>
<Label>Radar 1</Label>
<Model>Radar123</Model>
<Category>Civil</Category>
</Identification>
<Database>
<Radar456General>
<Identifier>203</Identifier>
<Version>8</Version>
<RadarStartStop>false</RadarStartStop>
<AntennaRotationSpeed>false</AntennaRotationSpeed>
<RadarAntennaRotation>false</RadarAntennaRotation>
<AntennaStaringPosition>0</AntennaStaringPosition>
</Radar456General>
</Database>
</Radar>
</Radars>
I am using XmlSerializer to deserialize the above message like this:
public static T Deserialize<T>(string message)
{
object obj = null;
try
{
XmlSerializer xs = new XmlSerializer(typeof(T));
StringReader reader = new StringReader(message);
XmlTextReader xmlReader = new XmlTextReader(reader);
obj = xs.Deserialize(xmlReader);
xmlReader.Close();
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw ex;
}
return (T)obj;
}
Radars radars = Serializer.Deserialize<Radars>(message);
After deserializing it, I get an empty Database section (meaning radars.radar[0].Database.Length == 0).
The schema of the message is specified in a separate XSD file. I use xsd.exe to generate C# classes from it.
This is the partial content of the XSD file.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://mycompany.com/2008/c2s2"
targetNamespace="http://mycompany.com/2008/c2s2"
elementFormDefault="qualified" version="1.0">
<xs:include schemaLocation="asterix_cat034.xsd"/>
<xs:include schemaLocation="asterix_cat253.xsd"/>
<xs:element name="Radars">
<xs:annotation>
<xs:documentation>List of radar information</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="Radar" type="RadarType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="RadarType">
<xs:sequence>
<xs:element name="Identification" type="RadarIdentificationType"/>
<xs:element name="Database" type="DatabaseType"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Radar" type="RadarType"/>
<xs:complexType name="RadarIdentificationType">
<xs:annotation>
<xs:documentation>
This type represents the radar identification.
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="Identifier" type="xs:string"/>
<xs:element name="IP" type="xs:string"/>
<xs:element name="Label" type="xs:string"/>
<xs:element name="Model" type="RadarModelType"/>
<xs:element name="Category" type="RadarCategoryType"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="RadarModelType">
<xs:restriction base="xs:string">
<xs:enumeration value="Radar123"/>
<xs:enumeration value="Radar456"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="RadarCategoryType">
<xs:restriction base="xs:string">
<xs:enumeration value="Civil"/>
<xs:enumeration value="Military"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="DatabaseSectionType">
<xs:sequence>
<xs:element name="Identifier" type="xs:unsignedByte"/>
<xs:element name="Version" type="I253_100_DB_VER"/>
</xs:sequence>
</xs:complexType>
<xs:element name="DatabaseSectionType" type="DatabaseSectionType">
<xs:annotation>
<xs:documentation>root of the substitution group</xs:documentation>
</xs:annotation>
</xs:element>
<xs:complexType name="DatabaseType">
<xs:sequence>
<xs:element ref="DatabaseSectionType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Radar456General" substitutionGroup="DatabaseSectionType">
<xs:annotation>
<xs:documentation>Radar456General database section</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:complexContent>
<xs:extension base="DatabaseSectionType">
<xs:sequence>
<xs:element name="RadarStartStop" type="I253_100_Start_Stop"/>
<xs:element name="AntennaRotationSpeed" type="I253_100_ARS"/>
<xs:element name="RadarAntennaRotation" type="I253_100_ROT"/>
<xs:element name="AntennaStaringPosition" type="I253_100_ASP"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:element name="Radar123General" substitutionGroup="DatabaseSectionType">
<xs:annotation>
<xs:documentation>Radar123General database section</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:complexContent>
<xs:extension base="DatabaseSectionType">
<xs:sequence>
<xs:element name="Shutdown" type="I253_100_Shutdown"/>
<xs:element name="RotationTransmission" type="I253_100_ROT_TX"/>
<xs:element name="SurfaceInstrumentedRange" type="I253_100_SIR"/>
<xs:element name="TransmitterTuneMode" type="I253_100_TT_mode"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
So how can I deserialize the message with Database section?
Try create the same object structure in code, serialize it and see he differences between your data and serialized date, than you'll know whats wrong.
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.