I'm doing a little R&D. I've got a simple object:
public class AccountNullable
{
private int? _AcctID;
private string _AcctName;
[XmlElementAttri bute(IsNullable = true)]
public int? AcctID
{
get { return _AcctID; }
set { _AcctID = value; }
}
public string AcctName
{
get { return _AcctName; }
set { _AcctName = value; }
}
}
I want to be able to serialize this object and have any property that
is null be left out of the resulting xml.
For example, the result of
AcctName = null
AcctID = 1234
would be
<AccountNullabl e>
<AcctID>1234</AcctID>
</AccountNullable >
and, the result of
AcctName = "Joe Programmer"
AcctID = null //this is a nullable integer type
would be
<AccountNullabl e>
<AcctName>Joe Programmer</AcctName>
</AccountNullable >
However, when the result I get when AcctID is set to null is this:
<AccountNullabl e>
<AcctID xsi:nil="true"/>
<AcctName>Joe Programmer</AcctName>
</AccountNullable >
Is it possible to serialize this object so that the propertys that are
null are excluded from the serialized xml instead of included with the
xsi:nil="true" attribute? Any help would be appreciated.
BTW, here is the code I used to test:
class Program
{
static void Main(string[] args)
{
try
{
AccountNullable an = new AccountNullable ();
Serialize(an);
an.AcctName = "Jane Dough";
Serialize(an);
an.AcctID = 12345;
Serialize(an);
Console.ReadLin e();
}
catch (Exception ex)
{
Log(String.Form at("{0}, {1}", ex.Message,
ex.StackTrace)) ;
}
Console.ReadLin e();
}
private static void Serialize(Accou ntNullable a)
{
StringWriter wr = new StringWriter();
XmlSerializer sr = new
XmlSerializer(t ypeof(AccountNu llable));
//Serialize object into an XML string
sr.Serialize(wr , a);
string xml = wr.ToString();
wr.Close();
Log(xml);
}
private static void Log(string msg)
{
Console.WriteLi ne(String.Forma t("{0}{1}", msg,
Environment.New Line));
}
} 3 10726
What happens if you leave out the IsNullable = true? (I don't have 2.0 on
the machine I'm on right now so can't check how 2.0 handles this.)
The following will certainly do the type of thing you are looking for
though:
public class AccountNullable
{
private int? _AcctID;
public int? AcctID
{
get { return _AcctID; }
set { _AcctID = value; }
}
[XmlIgnore]
public bool AcctIDSpecified
{
get
{
return AcctID.HasValue ;
}
}
}
By adding a bool property for each property XXX of the form XXXSpecified
that returns XXX.HasValue, the framework will not serialize the property if
it is null.
<ch************ ***@gmail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. . I'm doing a little R&D. I've got a simple object:
public class AccountNullable { private int? _AcctID;
private string _AcctName;
[XmlElementAttri bute(IsNullable = true)] public int? AcctID { get { return _AcctID; } set { _AcctID = value; } }
public string AcctName { get { return _AcctName; } set { _AcctName = value; } } }
I want to be able to serialize this object and have any property that is null be left out of the resulting xml.
For example, the result of
AcctName = null AcctID = 1234
would be
<AccountNullabl e> <AcctID>1234</AcctID> </AccountNullable >
and, the result of
AcctName = "Joe Programmer" AcctID = null //this is a nullable integer type
would be
<AccountNullabl e> <AcctName>Joe Programmer</AcctName> </AccountNullable >
However, when the result I get when AcctID is set to null is this:
<AccountNullabl e> <AcctID xsi:nil="true"/> <AcctName>Joe Programmer</AcctName> </AccountNullable >
Is it possible to serialize this object so that the propertys that are null are excluded from the serialized xml instead of included with the xsi:nil="true" attribute? Any help would be appreciated.
BTW, here is the code I used to test:
class Program { static void Main(string[] args) { try { AccountNullable an = new AccountNullable (); Serialize(an);
an.AcctName = "Jane Dough"; Serialize(an);
an.AcctID = 12345; Serialize(an);
Console.ReadLin e(); } catch (Exception ex) { Log(String.Form at("{0}, {1}", ex.Message, ex.StackTrace)) ; }
Console.ReadLin e(); }
private static void Serialize(Accou ntNullable a) { StringWriter wr = new StringWriter(); XmlSerializer sr = new XmlSerializer(t ypeof(AccountNu llable));
//Serialize object into an XML string sr.Serialize(wr , a); string xml = wr.ToString(); wr.Close();
Log(xml);
}
private static void Log(string msg) { Console.WriteLi ne(String.Forma t("{0}{1}", msg, Environment.New Line)); } }
Just looking at the 1.1 docs, says that if IsNullable = false then no XML
element is generated if the value is a null reference. Is not clear from the
docs what the default is, but presumably false.
"Clive Dixon" <cl************ *******@digita. noluncheonmeat. com> wrote in
message news:Ot******** ********@TK2MSF TNGP09.phx.gbl. .. What happens if you leave out the IsNullable = true? (I don't have 2.0 on the machine I'm on right now so can't check how 2.0 handles this.) The following will certainly do the type of thing you are looking for though:
public class AccountNullable { private int? _AcctID;
public int? AcctID { get { return _AcctID; } set { _AcctID = value; } }
[XmlIgnore] public bool AcctIDSpecified { get { return AcctID.HasValue ; } } }
By adding a bool property for each property XXX of the form XXXSpecified that returns XXX.HasValue, the framework will not serialize the property if it is null.
<ch************ ***@gmail.com> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.com.. . I'm doing a little R&D. I've got a simple object:
public class AccountNullable { private int? _AcctID;
private string _AcctName;
[XmlElementAttri bute(IsNullable = true)] public int? AcctID { get { return _AcctID; } set { _AcctID = value; } }
public string AcctName { get { return _AcctName; } set { _AcctName = value; } } }
I want to be able to serialize this object and have any property that is null be left out of the resulting xml.
For example, the result of
AcctName = null AcctID = 1234
would be
<AccountNullabl e> <AcctID>1234</AcctID> </AccountNullable >
and, the result of
AcctName = "Joe Programmer" AcctID = null //this is a nullable integer type
would be
<AccountNullabl e> <AcctName>Joe Programmer</AcctName> </AccountNullable >
However, when the result I get when AcctID is set to null is this:
<AccountNullabl e> <AcctID xsi:nil="true"/> <AcctName>Joe Programmer</AcctName> </AccountNullable >
Is it possible to serialize this object so that the propertys that are null are excluded from the serialized xml instead of included with the xsi:nil="true" attribute? Any help would be appreciated.
BTW, here is the code I used to test:
class Program { static void Main(string[] args) { try { AccountNullable an = new AccountNullable (); Serialize(an);
an.AcctName = "Jane Dough"; Serialize(an);
an.AcctID = 12345; Serialize(an);
Console.ReadLin e(); } catch (Exception ex) { Log(String.Form at("{0}, {1}", ex.Message, ex.StackTrace)) ; }
Console.ReadLin e(); }
private static void Serialize(Accou ntNullable a) { StringWriter wr = new StringWriter(); XmlSerializer sr = new XmlSerializer(t ypeof(AccountNu llable));
//Serialize object into an XML string sr.Serialize(wr , a); string xml = wr.ToString(); wr.Close();
Log(xml);
}
private static void Log(string msg) { Console.WriteLi ne(String.Forma t("{0}{1}", msg, Environment.New Line)); } }
Thank you so much Clive. I was running into problems with my
combination of IsNullable and XXX specified. I also think I might have
missed the .HasValue originally. In any case, you answered my
question. Many thanks :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John Wood |
last post by:
I was just looking at an article about using nullable value types. (value
types that can effectively have no value and not be set).
The syntax is to append a question-mark to the value type in the
declaration, eg:
int? age;
I don't like that much, I think it would be much more consistent to use a
new keyword, such as "nullable". But anyways...
|
by: Steven Livingstone |
last post by:
Bit of advice here folks.
I am creating a default constructor that just initializes a new instance of
my object and a secon constructor that takes an ID and loads an object with
data from the database for the given ID. I could just leave two separate
constructors, or i could just have the default one call the second with a
"-1" as the ID. This means that the initialization of certain fields can be
based on whether my ID is -1 (for lazy...
|
by: shawnk |
last post by:
Given several nullable boolean flags;
bool? l_flg_01 = true;
bool? l_flg_02 = false;
bool? l_flg_03 = true;
bool? l_result_flg = null;
I would have liked one of these syntax formats to work;
// if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
|
by: Larry Lard |
last post by:
There seems to be something a bit lacking in the way the dataset
designer thing deals (or rather doesn't) with nullable fields in
VS2005. Maybe it's cos I'm using VB2005 Express (which is variously
crippled), I don't know. But it seems to me obvious that there should
be some way to specify that DB fields which can be null should be
mapped to appropriate Nullable(Of T) generic types. For fields there's
a dropdown in the dataset designer...
|
by: Joe Bloggs |
last post by:
Hi,
Can someone please kindly show me how to determine if a type (read value
type) is Nullable.
MSDN has this KB:
How to: Identify a Nullable Type (C# Programming Guide)
http://msdn2.microsoft.com/en-us/library/ms366789.aspx
however, using their code snippet, I couldn't get it to work:
| |
by: Sam Kong |
last post by:
Hello,
I want to define a generic class which should accept only nullable
types or reference types.
What's the best way to costrain it?
---------
class MyClass<T>{
...
|
by: Mike P |
last post by:
What are nullable types used for? The only use I can think of is where
I have a method like this where some values being passed to it may be
null :
public int AddUser(int? UserRegion, string UserName etc)
You might have an Add User page where some of the fields do not have a
value entered by the user, so you can account for this by making these
types nullable in the method above. Is this the right usage of nullable
types? And what...
|
by: MobileBoy36 |
last post by:
Hi all,
Nullable types were announced as new handy stuff in .NET 2.0
But it seems like the datareader doesn't support nullable types.
You have still to check for "IsDbNull".
So, are Nullable Types useful at this moment? Do you get any benefit using
them (at this moment)?
Is Microsoft going to change this?
Best regards,
|
by: Tony Johansson |
last post by:
Hello!
I'm reading in a book called Visual C# 2005. In the chapter about Generics
there ia a section about Nullable types.
Here is the text that isn't complete true I think. It says:
"You can also look at the value of a reference type using the Value
property. If HasValue is true, then
you are guarantee a non-null value for Value, but if HasValue is false,
that is, null has been assigned to the variable, then accessing Value will
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |