带有null元素的Xmlserialization [英] Xmlserialization with null elements
问题描述
我有一个基于我生成架构类的xml架构。
我使用的是xmlserializer,我试图添加
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:
<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:
<Info>
<Fl>
<Name>Test</Name>
<News/>
</Fl>
</Info>
这是我的C#代码:
This is my C# code :
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;
}
这是我的xsd:
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?
推荐答案
而不是
Instead of
nillable="true"
尝试
try
minOccurs="0"
这将省去元素所有在一起。
This will leave out the element all together.
<Info>
<Fl>
<Name>Test</Name>
</Fl>
</Info>
您好Lalyka
您可以通过两种方式处理..要么在XSD中设置默认值< br $>
Hi Lalyka
you can handle in two ways .. either set default value in XSD
<xs:element default="" name="News" type="xs:string" />
或
您可以将这些类变量初始化为空字符串
问候
Praveen
or
you can initialise those class variables to empty string
Regards
Praveen
这篇关于带有null元素的Xmlserialization的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!