Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a xml schema that based on that I have generated the schema class.
I am using xmlserializer and I have tried to add
nillable="true"
so that it shows me the elements that are not null.

What I have tried:

it shows me the result like this:

XML
<Info>
  <Fl>
    <Name>Test</Name>
    <News d3p1:nil="true" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance"/>
  </Fl>
</Info>


but I want the result like this:

XML
<Info>
  <Fl>
    <Name>Test</Name>
    <News/>
  </Fl>
</Info>



This is my C# code :
C#
Info resp = new Info();
resp.Fl = data;
var serializer = new XmlSerializer(typeof(Info));
using (var stream = new StreamWriter("E:\\report.xml"))
{
	XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
	namespaces.Add(string.Empty, string.Empty);
	
	serializer.Serialize(stream, resp, namespaces);
	bRet = true;
}


and this my xsd :

<xs:schema id="Response" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="Info">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Fl" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="Name" nillable="true"/>
              <xs:element type="xs:string" name="News" nillable="true"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>



How can I have the result I am expecting?
Posted
Updated 31-May-16 11:13am

Instead of
XML
nillable="true"

try
XML
minOccurs="0"

This will leave out the element all together.
XML
<Info>
  <Fl>
    <Name>Test</Name>
  </Fl>
</Info>
 
Share this answer
 
v2
Hi Lalyka

you can handle in two ways .. either set default value in XSD


<xs:element default="" name="News" type="xs:string" />


or

you can initialise those class variables to empty string

Regards
Praveen
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900