Check XML file if it has the same structure - c#

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
}
}
}
}
}

Related

XSD2Code - Create a List of Objects when generate a class

I've created this XSD schema and I want to use XSD2Code to generate code. The problem I'm facing happens when I try to have a list of objects. It cannot recognize the complex type.
XSD Example:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="INVOICE01">
<xs:complexType>
<xs:sequence>
<xs:element name="Test" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="nome" minOccurs="0" type="xs:string" ></xs:element>
<xs:element name="nome2" minOccurs="0" type="xs:string" ></xs:element>
<xs:element name="henrique">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="henrique1" minOccurs="0"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
If I don't use the maxOccurs="unbounded" it work fine.

Using XSD in a WCF web service generating extra tags

I have a xsd file that defines the request of my service. So I used xsd.exe to generate a c# class for this service.
This is the xsd file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="schemaConectividade">
<xs:complexType>
<xs:choice>
<xs:element name="conectividadeParaOperadora" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="cabecalhoTransacao" type="ct_cabecalhoTransacao"/>
<xs:element name="dadosSolicitacao">
<xs:complexType>
<xs:sequence>
<xs:element name="atendimentoRN" type="xs:string"/>
<xs:element name="beneficiario">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="20"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="executante" type="xs:long"/>
<xs:element name="solicitante" type="xs:long"/>
<xs:element name="profissionalSolicitante" type="ct_profissionalSolicitante"/>
<xs:element name="caraterAtendimento" type="xs:int"/>
<xs:element name="procedimentosSolicitados">
<xs:complexType>
<xs:sequence>
<xs:element name="procedimento" type="ct_procedimento" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="operadoraParaConectividade" minOccurs="0">
<xs:complexType>
<xs:choice>
<xs:element name="retornoSchema" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="descricaoSchemaErro" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="retornoNegocio" type="ct_retornoNegocio" minOccurs="0"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="ct_dadosTransacao">
<xs:sequence>
<xs:element name="horaTransacao" type="xs:time"/>
<xs:element name="dataTransacao" type="xs:date"/>
<xs:element name="numeroTransacao" type="xs:long"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ct_retornoNegocio">
<xs:sequence>
<xs:element name="statusRetorno">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="dadosTransacao" type="ct_dadosTransacao"/>
<xs:element name="transacaoErro" type="ct_transacaoErro" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ct_transacaoErro">
<xs:sequence>
<xs:element name="descricaoErro" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ct_cabecalhoTransacao">
<xs:sequence>
<xs:element name="numeroTransacao" type="xs:long"/>
<xs:element name="numeroAtendimento" type="xs:long"/>
<xs:element name="dataTransacao" type="xs:date"/>
<xs:element name="horaTransacao" type="xs:time"/>
<xs:element name="tipoTransacao" type="xs:integer"/>
<xs:element name="terminal" type="xs:long" minOccurs="0"/>
<xs:element name="tipoTerminal" type="xs:string" minOccurs="0"/>
<xs:element name="fonteDadosBeneficiario" type="xs:string" minOccurs="0"/>
<xs:element name="fonteDadosPrestador" type="xs:string" minOccurs="0"/>
<xs:element name="centralAtendimento" type="xs:long" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ct_profissionalSolicitante">
<xs:sequence>
<xs:element name="nomeProfissional" type="xs:string"/>
<xs:element name="conselhoProfissional" type="xs:string"/>
<xs:element name="numeroConselhoProfissional" type="xs:int"/>
<xs:element name="UF" type="xs:string"/>
<xs:element name="CBOS" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ct_procedimento">
<xs:sequence>
<xs:element name="sequencialProcedimento" type="xs:integer"/>
<xs:element name="dataAtendimento" type="xs:date"/>
<xs:element name="quantidade" type="xs:integer"/>
<xs:element name="procedimento" type="xs:string"/>
<xs:element name="situacaoTransacao" type="xs:string"/>
<xs:element name="mensagem" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
and this is the interface generated:
using System.ServiceModel;
namespace MyNamespace
{
[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface IOrizon
{
[System.ServiceModel.OperationContract]
[System.ServiceModel.XmlSerializerFormatAttribute()]
ResponseType TransacaoAex_Operation(RequestType conectividadeParaOperadora);
}
}
When using SoapUI to test this service, it generates a "TransacaoAex_Operation" tag, that is not in my xsd file:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<TransacaoAex_Operation>
<conectividadeParaOperadora>
...
There is a way to avoid this?

How can I display IEnumerable<XElement> type in GridView?

I need to retrieve data from an XML file then display this result set in GridView by using LINQ.
I have read the XML file from disk and fetched the elements I want, but I need to display these results in table. What should I do?
C# code:
protected void Page_Load(object sender, EventArgs e)
{
XElement xml = XElement.Load(#"C:\veri.xml");
IEnumerable<XElement> urunler = xml.Elements("urun");
}
My xml schema :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="urun"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="urun">
<xs:complexType>
<xs:sequence>
<xs:element ref="urunid"/>
<xs:element ref="urunkodu"/>
<xs:element ref="urunadi"/>
<xs:element ref="urunlink"/>
<xs:element ref="urundetay"/>
<xs:element ref="marka"/>
<xs:element ref="urunkategori"/>
<xs:element ref="kategorinav"/>
<xs:element ref="resimler"/>
<xs:element ref="fiyat"/>
<xs:element ref="toplamstok"/>
<xs:element ref="stokbilgileri"/>
<xs:element ref="ozellikler"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="urunid" type="xs:integer"/>
<xs:element name="urunkodu" type="xs:string"/>
<xs:element name="urunadi" type="xs:string"/>
<xs:element name="urunlink" type="xs:anyURI"/>
<xs:element name="urundetay" type="xs:string"/>
<xs:element name="marka">
<xs:complexType>
<xs:sequence>
<xs:element ref="markaadi"/>
<xs:element ref="markaid"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="markaadi" type="xs:string"/>
<xs:element name="markaid" type="xs:integer"/>
<xs:element name="urunkategori">
<xs:complexType>
<xs:sequence>
<xs:element ref="kategoriadi"/>
<xs:element ref="kategoriid"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="kategoriadi" type="xs:string"/>
<xs:element name="kategoriid" type="xs:integer"/>
<xs:element name="kategorinav">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="kategori"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="kategori" type="xs:string"/>
<xs:element name="resimler">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="resim"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="resim" type="xs:anyURI"/>
<xs:element name="fiyat">
<xs:complexType>
<xs:sequence>
<xs:element ref="kdvoran"/>
<xs:element ref="bayifiyat"/>
<xs:element ref="bayifiyatkdvli"/>
<xs:element ref="perakendefiyat"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="kdvoran" type="xs:integer"/>
<xs:element name="bayifiyat" type="xs:string"/>
<xs:element name="bayifiyatkdvli" type="xs:string"/>
<xs:element name="perakendefiyat" type="xs:string"/>
<xs:element name="toplamstok" type="xs:integer"/>
<xs:element name="stokbilgileri">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="stokkayit"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="stokkayit">
<xs:complexType>
<xs:sequence>
<xs:element ref="renk"/>
<xs:element ref="beden"/>
<xs:element ref="stokadet"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="renk" type="xs:string"/>
<xs:element name="beden" type="xs:NMTOKEN"/>
<xs:element name="stokadet" type="xs:integer"/>
<xs:element name="ozellikler">
<xs:complexType>
<xs:sequence>
<xs:element ref="aynigunkargo"/>
<xs:element ref="hizligonderi"/>
<xs:element ref="onsiparisliurun"/>
<xs:element ref="sinirlistok"/>
<xs:element ref="ucretsizkargo"/>
<xs:element ref="stoksorunuz"/>
<xs:element ref="desi"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="aynigunkargo" type="xs:NCName"/>
<xs:element name="hizligonderi" type="xs:NCName"/>
<xs:element name="onsiparisliurun" type="xs:NCName"/>
<xs:element name="sinirlistok" type="xs:NCName"/>
<xs:element name="ucretsizkargo" type="xs:NCName"/>
<xs:element name="stoksorunuz" type="xs:NCName"/>
<xs:element name="desi" type="xs:integer"/>
</xs:schema>

Error adding schema to XmlSchemaSet

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

Problem deserializing xml file

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.

Categories

Resources