470,841 Members | 1,078 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

Wanting to not add a blank element to the 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


Apr 10 '06 #1
6 1390
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."

Apr 11 '06 #2
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("xs d", "2.0.50727.42")]

// [System.SerializableAttribute()]

[System.Diagnostics.DebuggerStepThroughAttribute()]

[System.ComponentModel.DesignerCategoryAttribute("c ode")]

[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=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.
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:88**************@TK2MSFTNGXA01.phx.gbl...
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."

Apr 11 '06 #3
I've forgot to specify that this is on the compact framework.

"jamie" <st*********@nospam.nospam> wrote in message
news:eS**************@TK2MSFTNGP04.phx.gbl...
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("xs d", "2.0.50727.42")]

// [System.SerializableAttribute()]

[System.Diagnostics.DebuggerStepThroughAttribute()]

[System.ComponentModel.DesignerCategoryAttribute("c ode")]

[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=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.
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:88**************@TK2MSFTNGXA01.phx.gbl...
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."


Apr 11 '06 #4
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."

Apr 12 '06 #5
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
First are you an XMLWriter or using a DataSet or DataTable to serialize via WriteXml? Reason I ask is the answer is different depending.
Apr 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by charly | last post: by
64 posts views Thread by Chris Rodriguez | last post: by
2 posts views Thread by Danny | last post: by
2 posts views Thread by mjwills | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.