0

I use Xsd2Code to generate classes. However one of the classes causes me a problem with its constructor, because of the loop and throws StackOverflowException.

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.4.0.32990")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public partial class ApproverType : INotifyPropertyChanged
{
    private ApproverType replacesField;

    public ApproverType()
    {
        this.replacesField = new ApproverType();
    }

    public ApproverType Replaces
    {
        get
        {
            return this.replacesField;
        }
        set
        {
            if ((this.replacesField != null))
            {
                if ((replacesField.Equals(value) != true))
                {
                    this.replacesField = value;
                    this.OnPropertyChanged("Replaces");
                }
            }
            else
            {
                this.replacesField = value;
                this.OnPropertyChanged("Replaces");
            }
        }
    }
}
v.g.
  • 1,076
  • 5
  • 19
  • 38

2 Answers2

1

How is the Replaces property diclared in your XSD? Probably it's a required element, and the tool generates an instance to comply with the contract.

You could then try to change the definition, making it optional.

If it's something like:

<xs:element name="Replaces" type="ApproverType" use="required" />

You then have to change it to:

<xs:element name="Replaces" type="ApproverType" use="optional" />

EDIT: A scheme would be incorrect anyway, because a recursive required element is impossible in xml too:

<ApproverType>
 <Replaces>
   <Replaces>
     <Replaces>
       <Replaces>
        ... infinite
 <Replaces>
 <OtherProperty />
<ApproverType>

EDIT:

One possible workaround would be having a backing property as a collection of single element:

public List<ApproverType> ReplacesWorkaround { ... }

in another part of partial class:

public ApproverType Replaces
{
 get
 {
  return ReplacesWorkaround.SingleOrDefault();
 }
 set
 {
  ReplacesWorkaround.RemoveAll();
  ReplacesWorkaround.Add(value);
 }
}
George Polevoy
  • 7,450
  • 3
  • 36
  • 61
0

The problem is there are other classes like this.. Obviously this tool is somehow buggy. https://xsd2code.codeplex.com/workitem/7419

I am using version 3.4.0.32990. Is there a way to fix this, easily

v.g.
  • 1,076
  • 5
  • 19
  • 38