Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6155605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:27:48+00:00 2026-05-23T20:27:48+00:00

I got the following code: public class Alarm:IDisposable { MemoryStream _tmp; readonly XmlDocument _doc;

  • 0

I got the following code:

public class Alarm:IDisposable
{
    MemoryStream _tmp;
    readonly XmlDocument _doc;
    private readonly XmlSerializerNamespaces _ns;
    private readonly XmlSerializer _x = new  XmlSerializer(typeof(Alarm));
    public int? ID { get; set; }
    public string SourceSystem { get; set; }
    public string SensorName { get; set; }
    public string ModelName { get; set; }
    public int? Severity { get; set; }
    public int? Duration { get; set; }
    public bool? Status { get; set; }
    public DateTime? StartTime { get; set; }
    public DateTime? EndTime { get; set; }

    public Alarm()
    {
        _tmp = new MemoryStream();
        _doc = new XmlDocument();
        _ns = new XmlSerializerNamespaces();
        _ns.Add("", "");
    }
    public string OuterXml()
    {

        //Add an empty namespace and empty value
        _x.Serialize(_tmp, this, _ns);
        _tmp.Position = 0;
        _doc.Load(_tmp);
        return _doc.OuterXml;
    }


    public void Dispose()
    {
        if (_tmp!=null)
        {
            _tmp.Close();
            _tmp = null;
        }

    }
}

I get as output“

 <?xml version=\"1.0\"?>
 <Alarm><ID>1</ID><SourceSystem>HMer</SourceSystem>
 <SensorName>4</SensorName><Severity d2p1:nil=\"true\" xmlns:d2p1=\"http://www.w3.org/2001/XMLSchema-instance\" />
 <Duration>500</Duration><Status>true</Status>
 <StartTime>2011-07-19T12:29:51.171875+03:00</StartTime>
<EndTime d2p1:nil=\"true\" 
 xmlns:d2p1=\"http://www.w3.org/2001/XMLSchema-instance\" /></Alarm>

I wanna get:

<Alarm><ID>1</ID><SourceSystem>HMer</SourceSystem><SensorName>4</SensorName>
<Duration>500</Duration><Status>true</Status>
<StartTime>2011-07-19T12:29:51.171875+03:00</StartTime></Alarm>

meaning no xmlns stuff, no tag where value is null.

please assist meh

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:27:49+00:00Added an answer on May 23, 2026 at 8:27 pm

    Add the following properties to your Nullable fields:

    [XmlIgnore]
    public bool EndTimeSpecified { get { return EndTime.HasValue; } }
    

    This lets XmlSerializer (which is a bit dumb about Nulllable fields) realize that no EndTime has been specified, and, thus, the field does not need to be serialized.

    Here’s the documentation of this feature:

    Another option is to use a special pattern to create a Boolean field recognized by the XmlSerializer, and to apply the XmlIgnoreAttribute to the field. The pattern is created in the form of propertyNameSpecified. For example, if there is a field named “MyFirstName” you would also create a field named “MyFirstNameSpecified” that instructs the XmlSerializer whether to generate the XML element named “MyFirstName”.

    Here’s a related SO question that offers more details and explanations:

    • How to make a value type nullable with .NET XmlSerializer?

    About removing the XML processing instruction (i.e., the <?xml ... ?> part), there’s also a SO question about that:

    • Omitting XML processing instruction when serializing an object
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
I'v got the following code : public class MyModule: IModule { private IRegionManager mRegionManager
I've got the following code : public class A { ~A() { Console.WriteLine(destructor); }
I just got to read the following code somewhere : public class SingletonObjectDemo {
I've got the following code: public IList<IProductViewModel> ChildProducts { get; set; } public IList<IProductViewModel>
I got the following code inside my templateApp.java: package com.android.templateApp; import android.app.Activity; import android.os.Bundle;
Hi all i've got the following code which will go off and get all
I don't understand what is going on here... I've got the following error: The
public class DTOa { public int Id { get; set;} public string FirstName {
I'm try to insert or update db record with following code: Category category =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.