473,394 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1477
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: JDS | last post by:
Hi, all. I'd like to do the following, preferably *without* resorting to JavaScript: I have a long, dynamically-generated form questionnaire. Not all of the form fields are dynamically...
6
by: charly | last post by:
Greetings, I'm trying my hand at xml and css (no, I don't want to use XSL yet). So I got a test.xml : <?xml version="1.0" encoding="ISO-8859-1" ?> <?xml-stylesheet href="styles.css"...
64
by: Chris Rodriguez | last post by:
after creating a couple of mediocre sites and talking too much about them, i've been overwhelmed w/ requests for new sites & site make-overs. i have a full-time job and i don't have the time or...
2
by: Danny | last post by:
Is there a way I can get rid of a blank like under an <h3></h3> tag? without using css Thanks in advance
2
by: mjwills | last post by:
All, I am using VB.NET 1.0 / 2002. I am having trouble setting a column default to "" (note that "" is not the same as Null / DBNull / Nothing) in a typed dataset (to reflect the same database...
4
by: rob c | last post by:
This is a minor thing and only appears in IE (so far), but I'd like to know to correct it (if possible). Whenever I use a form on a webpage, Explorer always leaves a blank line following the...
7
by: beachdog | last post by:
I'm using Visual Studio 2005/C# to build a web client. The web server is something I've written in a different framework, which does not support generating wsdl, so I have hand-built a wsdl file,...
4
by: BibhuAshish | last post by:
Hi, I wanted to delete a line from xml file which i did it. But after deletion of that line there is a blank space. Again if i am adding another line by using java that blank line remains as...
4
by: Patrick Nolan | last post by:
I am using javascript to manipulate optgroups in select elements. When the results are displayed in Firefox 2.0 there is an annoying blank line at the top of the multi-line select box. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.