StackOverFlowException when generating C# code from xsd using xsd.exe (VS2010) - c#

I am trying to generate C# code from an XML schema with xsd.exe with Visual Studio RC1 (version 10.0.30128.1) but get the follwoing error:
C:\Development>xsd CR2008Schema.xsd /classes
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30128.1]
Copyright (C) Microsoft Corporation. All rights reser
Process is terminated due to StackOverflowException.
The xsd is http://www.businessobjects.com/products/xml/CR2008Schema.xsd
Any help appreciated.
Thanks,
Staffan

This is probably happening because Group defines a collection of Group:
<!-- Group -->
<xsd:complexType name="Group">
<xsd:sequence>
<xsd:element name="GroupHeader" type="HeaderFooter" minOccurs="0"/>
<xsd:choice>
<xsd:element name="Details" type="Details" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="Group" type="Group" minOccurs="0" maxOccurs="unbounded"/>
</xsd:choice>
<xsd:element name="GroupFooter" type="HeaderFooter" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="Level" type="xsd:integer" use="required"/>
</xsd:complexType>
xsd.exe is getting into an infinite loop...

Further to Oded's answer there is a similar but more comples loop in the CrystalReport type. The Details element of type Details contains a SubReport element that is of type Subreport, that inherits from CrystalReport, which contains a Details element of type Details etc.

Related

C# xsd xmlreader, how to count attribute names

This is probably very simple for you guys, but I need to be able to count the attribute names within an xml document when validating xml against a schema using c#. Specifically duplicate attribute names (not valid).
If my xsd has these in it:
<xsd:schema>
<xsd:complexType name="scheduleEvent">
<xsd:all>
<xsd:element name="Basic" type="MyBasic"/>
</xsd:all>
</xsd:complexType>
<xsd:complexType name="MyBasic">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="Descriptor" type="Descriptor" maxOccurs="1"/>
<xsd:element name="Descriptor1" type="Descriptor1" maxOccurs="1"/>
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="Descriptor">
<xsd:attribute name="Test" type="typ:Test"/>
</xsd:complexType>
<xsd:complexType name="Descriptor1">
<xsd:attribute name="Test1" type="typ:Test1"/>
</xsd:complexType>
And my xml to validate against looks like (not valid, I know just a quick mock up example for reference:
Declarations etc...
<ScheduleEvent>
<MyBasic>
<Descriptor Test="02"/>
<Descriptor1 Test1="02" Test1="02"/>
</MyBasic>
</ScheduleEvent>
How can I count the number of "Test1" attributes? C# xmlreader (without using exceptions, long story).
First of all, that isn't a valid xml. An attribute is unique per element.
If what you ment is that you can have multiple "Test1" attributes throughout the different elements, then you can do the following using LINQ to XML:
var xml = XDocument.Load(#"PathToXml");
var testCount = xml.Descendants().Attributes("Test1").Count();
Try this :
XDocument doc=XDocument.Load(XMLFile or XMLFilePath);
var Sample=doc.Descendants("Test1").Count();

.NET SOAP serialization, omit/remove enclosing tags for generic list<t>

I need to call a Web Service, created by someone else, from a .Net C# application.
The web service requires me to send a list of items ("articles"), without enclosing them in a parent element. This is a simplified illustration of the "desired" look of the call:
<body>
<info>something</info>
<moreinfo>something else</moreinfo>
<article>
<articleno>123</articleno>
</article>
<article>
<articleno>456</articleno>
</article>
</body>
On my end, the articles is a generic List<Article>, and my call to the web service tends to contain something looking more like this:
<body>
<info>something</info>
<moreinfo>something else</moreinfo>
<articles><!-- parent element -->
<article>
<articleno>123</articleno>
</article>
<article>
<articleno>456</articleno>
</article>
</articles><!-- parent element end -->
</body>
The WSDL file from the web service is corrupted, so I've hade to manually create a working subset of the functions in it that I need. Using that "hand coded" WSDL I add a Service Reference using Visual Studio 2013.
I have been trying to solve the problem by modifying the WSDL, and by modifying the code that is created by Visual Studio when adding the service reference, but so far I haven't succeeded.
I've bee trying various attributes for the list parameter, such as XmlElement, XmlIgnore, MessageHeaderArrayAttribute and others, but to be honest I don't know how they work or which ones could actually be useful in this scenario.
Can the WSDL be altered to make Visual Studio's auto generated code create the correct output when calling the web service? (Preferred solution)
Or is there a way to force soap serialization to produce the list of articles without an enclosing element? (Acceptable solution)
Here is a simplified version of the relevant (as far as I can tell) portions of the WSDL
<xsd:element name="PriceRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="1" name="info" type="xsd:string" />
<xsd:element minOccurs="0" maxOccurs="1" name="moreinfo" type="xsd:string" />
<xsd:element minOccurs="0" maxOccurs="1" name="articles" type="tns:ArrayOfArticlestructObj" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
...
<xsd:complexType name="ArticlestructObj">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="1" name="articleno" type="xsd:string" />
<xsd:element minOccurs="0" maxOccurs="1" name="otherinfo..." type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="ArticlestructObj" type="tns:ArticlestructObj" />
<xsd:complexType name="ArrayOfArticlestructObj">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" name="ArticlestructObj" nillable="true" type="tns:Articlestruct" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="ArrayOfArticlestruct" type="tns:ArrayOfArticlestruct" />
I am a noob on WSDL as well as on StackOverflow, so please excuse me if this question is hard to read.

Validating XML content in XML-schema annotation/appinfo

My understanding of XML isn't exactly stunning, but my understanding of content in annotation/appinfo is that you're allowed any well-formed XML, and this is lax validated. My understanding of lax validation is that elements and attributes will be validated if the relevant schema information can be obtained.
My situation is that I have an XML schema with some content in this section that requires validating. I have the relevant schemas to validate the content against.
I've taken a totally simple XML schema and added an appinfo element to it, as shown below. The content of the appinfo is another element declaration, purely for simplicity of not referencing another schema. The schema I'm trying to validate obviously references something different.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.demo.org"
xmlns="http://www.demo.org"
elementFormDefault="qualified">
<xsd:annotation>
<xsd:appinfo>
<xsd:element name="Demo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="A" type="xsd:iteger"/>
<xsd:element name="B" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:appinfo>
</xsd:annotation>
<xsd:element name="Demo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="A" type="xsd:integer"/>
<xsd:element name="B" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Note in the appinfo section, I've deliberately mis-spelled "integer" on element A. If I do this in the main body of the schema, then XmlSchemaSet.Compile() will tell me this schema isn't valid.
However, if I use the schema as it's written above, it tells me there is no problem. In the more complex file I started with, I supplied the external schema to XmlSchemaSet via XmlSchemaSet.Add()
I've also tried loading the XML schema from http://www.w3.org/2001/XMLSchema.xsd and loading the schema to be validated into an XmlDocument and running XmlDocument.Validate() but no joy.
I feel like I'm totally missing something totally fundamental after spending hours on this. Any pointers appreciated!
This is not my understanding of lax: if you have a declaration
<any
namespace=". . . "
processContents="lax">
</any>
the content of the corresponding element in the XML being validated will be validated against the schemas corresponding to the namespace(s) specified by namespace (if any) - not against any schema it might be using. The declaration for appInfo is
<xs:element name="appinfo" id="appinfo">
<xs:complexType mixed="true">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:any processContents="lax"/>
</xs:sequence>
<xs:attribute name="source" type="xs:anyURI"/>
<xs:anyAttribute namespace="##other" processContents="lax"/>
</xs:complexType>
</xs:element>
without any namespace specification, that corresponds to namespace="##any", that means that there is no validation against any schema.

Consuming WSDL with multidimentional array with WSDL.exe fails

I have the following in a WSDL I am consuming;
<xsd:complexType name="SomeClassType">
<xsd:sequence>
<xsd:element type="xsd:string" name="errorMessage" minOccurs="1" nillable="true" maxOccurs="1"> </xsd:element>
<xsd:element type="tp:ArrayOfArrayOfString" name="values" minOccurs="1" nillable="true" maxOccurs="1"> </xsd:element>
<xsd:element type="xsd:boolean" name="isEmpty" minOccurs="1" maxOccurs="1"> </xsd:element>
</xsd:sequence>
</xsd:complexType>
where
<xsd:complexType name="ArrayOfArrayOfString">
<xsd:complexContent>
<xsd:restriction base="soapenc:Array">
<xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:string[,]"></xsd:attribute>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
However using wsdl.exe from MS (Runtime Version: 1.1.4322.573) generates
public class SomeClassType {
///
public string errorMessage;
///
public string[] values;
///
public bool isEmpty;
}
I expected string[,] values not string[] values
Is there a fix or a work around to this problem? (other than manually changing the generated code)
I had to set the type="tp:ArrayOfArrayOfString" to type="tp:ArrayOfString" and the maxOccurs="unbounded"
I believe you could try the WCF proxy generator (I believe WCF uses some other util, not wsdl.exe) - maybe that would be useful, but if that fails - I think that manually editing the generated code is your only option.
Try svcutil.exe. it is advisable to try a newer version of .net.

.NET xsd importer creates unserializable class

I am using the .NET XSD.EXE importer to generate C# classes from a collection of XSD files. When I tried to serialize one of the classes to XML it failed (InvalidOperationException), and when I dug into it I discovered it one of the created classes appears to be wrong.
Here is the pertinent XSD code:
<xsd:complexType name="SuccessType">
<xsd:annotation>
<xsd:documentation>Indicates in a response message that a request was successfully processed.</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="Warnings" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!-- .. snip .. -->
<xsd:element name="Warnings" type="WarningsType">
<xsd:annotation>
<xsd:documentation>The processing status of a business message and any related warnings or informational messages.</xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- .. snip .. -->
<xsd:complexType name="WarningsType">
<xsd:annotation>
<xsd:documentation>A collection of warnings generated by the successful processing of a business message.</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="Warning" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!-- .. snip .. -->
<xsd:element name="Warning" type="WarningType">
<xsd:annotation>
<xsd:documentation>Defines details of a warning that occurred during message processing.</xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- .. snip .. -->
<xsd:complexType name="WarningType">
<xsd:annotation>
<xsd:documentation>Defines details of a warning that occurred during message processing.</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="WarningCategory"/>
<xsd:element ref="WarningCode"/>
<xsd:element ref="WarningShortMessage"/>
<xsd:element ref="WarningMessage"/>
</xsd:sequence>
</xsd:complexType>
And here is the C# code generated from it:
public partial class SuccessType
{
private WarningType[][] warningsField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("Warning", typeof(WarningType), IsNullable = false)]
public WarningType[][] Warnings
{
get
{
return this.warningsField;
}
set
{
this.warningsField = value;
}
}
}
It made Warnings an array of an array of WarningType. When I attempt to serialize that to XML I get an InvalidOperationException exception:
Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'WarningType[]' to 'WarningType'
error CS0030: Cannot convert type 'WarningType[]' to 'WarningType'
error CS0029: Cannot implicitly convert type 'WarningType' to 'WarningType[]'
error CS0029: Cannot implicitly convert type 'WarningType' to 'WarningType[]'
But if I change the generated code from WarningType[][] to WarningType[] then it serializes fine.
Short of editing the generated C# class whenever I regenerate this (which hopefully will be less frequently going forward), is there any other solution? Is this a bug in xsd.exe or is the XSD file incorrect? Maybe there is a problem in the XmlSerializer?
What I want is C# code that correctly serializes to XML that validates against the XSD. Right now the jagged array seems to be wrong because if I remove it then it generates the XML.
I am using Visual Studio 2008.
There are bugs in the xsd.exe tool. I don't remember this particular one, but I do remember finding problems with jagged arrays, and it's possible this remains a bug. if you're willing, you could use the XsdObjbectGen tool, also from Microsoft, but released independently and out-of-band from the .NET SDK.
One thing you could do is go the reverse direction: write the C# code, then generate the schema with xsd.exe, and see what is different. It's possible xsd.exe wants the schema to look a particular way, in order to correctly generate correct code for jagged arrays.
Actually, upon re-reading your question, you never said what you really wanted. Do you want SuccessType to contain an array-of-arrays, or not?
And is it WarningsType or WarningType? There's some disagreement between the code snips you provided.
Assuming you wanted the array-of-arrays, I wrote this C# code:
public class WarningType
{
public String oof;
}
public partial class SuccessType
{
private WarningType[][] warningsField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("Warning", typeof(WarningType[]), IsNullable = false)]
public WarningType[][] Warnings
{
get
{
return this.warningsField;
}
set
{
this.warningsField = value;
}
}
}
... then compiled it into a DLL. Then I ran xsd.exe on that DLL, and generated this XSD:
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="WarningType" nillable="true" type="WarningType" />
<xs:complexType name="WarningType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="oof" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="SuccessType" nillable="true" type="SuccessType" />
<xs:complexType name="SuccessType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="Warnings" type="ArrayOfArrayOfWarningType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ArrayOfArrayOfWarningType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Warning" type="ArrayOfWarningType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ArrayOfWarningType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="WarningType" nillable="true" type="WarningType" />
</xs:sequence>
</xs:complexType>
</xs:schema>
...and it round-trips. If I then run xsd.exe on that schema, I get a type that wraps an array-of-arrays.

Categories