XSD2Code - Create a List of Objects when generate a class - c#

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.

Related

create index on XML file

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"]

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 to create child node using named types in XML

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>

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>

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