all groups > dotnet xml > april 2006 >
You're in the

dotnet xml

group:

Wanting to not add a blank element to the XML



Wanting to not add a blank element to the XML jamie
4/10/2006 12:36:57 PM
dotnet xml: I'm serializing a class and some of the elements end up with blank results.
This is fine. When I serialize the class the elements show up in the
resulting XML as something like
<SigningLicence/>

If it's blank how can I prevent this element from getting into the XML
completely . It is optional according to the schema but fails on validation
when it's in there in this form.

Jamie



RE: Wanting to not add a blank element to the XML v-kevy NO[at]SPAM online.microsoft.com
4/11/2006 2:13:11 AM
Hi Jamie,

Could you post some code of your class and the serialization code, so that
we can try to make a repro on this issue?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Re: Wanting to not add a blank element to the XML jamie
4/11/2006 10:39:00 AM
Sure, Here's a hacked up test case that recreates it

The serialization code

public Form1()

{

testClass test = new testClass();

InitializeComponent();

try

{

XmlSerializer x = new XmlSerializer(typeof(testClass));

TextWriter writer = new StreamWriter("\\Program Files\\log scale manager
2\\" + "HBS.xml");

x.Serialize(writer, test);

}

catch (Exception e)

{

MessageBox.Show(e.ToString());

}

}


The testClass code

namespace XML_test1{

using System.Xml.Serialization;

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Xml;

using System.Xml.Schema;

using System.IO;


[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]

// [System.SerializableAttribute()]

[System.Diagnostics.DebuggerStepThroughAttribute()]

[System.ComponentModel.DesignerCategoryAttribute("code")]

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]

public partial class testClass {



private string eventTypeField;

private string pSCommonField;

private string loadArrivalNumberField;




[System.Xml.Serialization.XmlElementAttribute(Order = 0)]

public string EventType

{

get

{ return this.eventTypeField; }

set

{ this.eventTypeField = value; }

}

[System.Xml.Serialization.XmlElementAttribute(Order = 1)]

public string PSCommon

{

get

{ return this.pSCommonField; }

set

{ this.pSCommonField = value; }

}

[System.Xml.Serialization.XmlElementAttribute(Order = 2)]

public string LoadArrivalNumber

{

get

{ return this.loadArrivalNumberField; }

set

{ this.loadArrivalNumberField = value; }

}



public testClass()

{

EventType = "1";

PSCommon = "";

LoadArrivalNumber = "3";


}


}


}

This results in

<?xml version="1.0" encoding="utf-8"?>

<testClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<EventType>1</EventType>

<PSCommon/>

<LoadArrivalNumber>3</LoadArrivalNumber>

</testClass>



I found that by checking if PSCommon.Trim() == "" and then setting it to
null if true prevents

<PSCommon/> from appearing.

Annoying hack but it works.


[quoted text, click to view]

Re: Wanting to not add a blank element to the XML jamie
4/11/2006 10:59:43 AM
I've forgot to specify that this is on the compact framework.

[quoted text, click to view]

Re: Wanting to not add a blank element to the XML v-kevy NO[at]SPAM online.microsoft.com
4/12/2006 12:26:54 PM
Hi Jamie,

This is by design. The element will always be generated, although it is a
blank field. If you really want to remove it, I think you have found a
workaround. But there might be error when deserializing the object.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Re: Wanting to not add a blank element to the XML dickster
4/13/2006 3:46:13 AM
Might be of use

http://groups.google.com/group/microsoft.public.dotnet.xml/browse_frm/thread/506ce052768f532d/4b45e4ea8a84a052?q=dickster+&rnum=20#4b45e4ea8a84a052

Dickster
AddThis Social Bookmark Button