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 7064699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:51:59+00:00 2026-05-28T04:51:59+00:00

I have an object generated by an Add service reference… operation and I am

  • 0

I have an object generated by an “Add service reference…” operation and I am manually serializing it with a generic serializer I wrote.

My problem is that the data contract has some internal objects.

The serializer adds an empty namespace atribute to the starting tag of the internal objects. Is there any way to stop this from happening?

  • 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-28T04:52:00+00:00Added an answer on May 28, 2026 at 4:52 am

    What about making your internal objects belong to the same namespace as the root? That way, it would be correct to omit the xmlns declaration from the descendants. You can use the [assembly: ContractNamespace] attribute to override the namespace for all contracts in your assembly. Refer to Data Contract Names for an example.

    Edit: An elaboration with some examples below.

    Suppose you’re constructing an XML document manually, and don’t specify a namespace for any of your elements.

    XDocument xmlDocument = new XDocument(
        new XElement("Book",
            new XElement("Title", "Animal Farm"),
            new XElement("Author", "George Orwell"),
            new XElement("Publisher",
                new XElement("Name", "Secker and Warburg"),
                new XElement("Location", "London"),
                new XElement("Founded", 1910))));
    return xmlDocument.ToString();
    

    The generated XML would, as expected, be void of namespace declarations:

    <Book>
      <Title>Animal Farm</Title>
      <Author>George Orwell</Author>
      <Publisher>
        <Name>Secker and Warburg</Name>
        <Location>London</Location>
        <Founded>1910</Founded>
      </Publisher>
    </Book>
    

    If, however, you specify a namespace just for your root element, then all child elements must explicitly revert out of that default namespace, using the xml="" declaration. Per the Namespace Defaulting rules:

    The scope of a default namespace declaration extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag, excluding the scope of any inner default namespace declarations. In the case of an empty tag, the scope is the tag itself.

    Thus, the following code (having a namespace specified for the root element)…

    XDocument xmlDocument = new XDocument(
        new XElement("{http://example.com/library}Book",
            new XElement("Title", "Animal Farm"),
            new XElement("Author", "George Orwell"),
            new XElement("Publisher",
                new XElement("Name", "Secker and Warburg"),
                new XElement("Location", "London"),
                new XElement("Founded", 1910))));
    return xmlDocument.ToString();
    

    …would give the following XML:

    <Book xmlns="http://example.com/library">
      <Title xmlns="">Animal Farm</Title>
      <Author xmlns="">George Orwell</Author>
      <Publisher xmlns="">
        <Name>Secker and Warburg</Name>
        <Location>London</Location>
        <Founded>1910</Founded>
      </Publisher>
    </Book>
    

    Note how the children of the <Publisher> element do not need to revert out of the root’s namespace, since they inherit the “no namespace” declaration from their parent.

    To eliminate the xmlns="" declarations, we could, for the sake of the demonstration, assign the same namespace to all descendants:

    XDocument xmlDocument = new XDocument(
        new XElement("{http://example.com/library}Book",
            new XElement("{http://example.com/library}Title", "Animal Farm"),
            new XElement("{http://example.com/library}Author", "George Orwell"),
            new XElement("{http://example.com/library}Publisher",
                new XElement("{http://example.com/library}Name", "Secker and Warburg"),
                new XElement("{http://example.com/library}Location", "London"),
                new XElement("{http://example.com/library}Founded", 1910))));
    return xmlDocument.ToString();
    

    This would give an XML document with the namespace declared in just the root (and implicitly inherited in all descendants):

    <Book xmlns="http://example.com/library">
      <Title>Animal Farm</Title>
      <Author>George Orwell</Author>
      <Publisher>
        <Name>Secker and Warburg</Name>
        <Location>London</Location>
        <Founded>1910</Founded>
      </Publisher>
    </Book>
    

    To mimic your scenario involving a web service, we can create the following WCF service.

    [DataContract]
    public class Book
    {
        [DataMember]
        public string Title { get; set; }
        [DataMember]
        public string Author { get; set; }
        [DataMember]
        public Publisher Publisher { get; set; }
    }
    
    [DataContract]
    public class Publisher
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Location { get; set; }
        [DataMember]
        public short Founded { get; set; }
    }
    
    [ServiceContract]
    public interface ILibraryService
    {
        [OperationContract]
        Book GetBook();
    }
    
    public class LibraryService : ILibraryService
    {
        public Book GetBook()
        {
            return new Book
            {
                Title = "Animal Farm",
                Author = "George Orwell",
                Publisher = new Publisher
                {
                    Name = "Secker and Warburg",
                    Location = "London",
                    Founded = 1910,
                }
            };
        }
    }
    

    We add a service reference to the above service in our client application, consume its operation, and serialize the result whilst enclosing it in a root Books element having an explicit namespace:

    using (var libraryClient = new LibraryServiceReference.LibraryServiceClient())
    {
        var book = libraryClient.GetBook();
    
        var stringBuilder = new StringBuilder();
        using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder))
        {
            xmlWriter.WriteStartElement("Books", "http://example.com/library");
            var serializer = new XmlSerializer(book.GetType());
            serializer.Serialize(xmlWriter, book);
            xmlWriter.WriteEndElement();
        }
    
        return stringBuilder.ToString();
    }
    

    In this case, the inner element Book contains an xmlns="" declaration.

    <?xml version="1.0" encoding="utf-16"?>
    <Books xmlns="http://example.com/library">
      <Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns="">
        <ExtensionData />
        <Author>George Orwell</Author>
        <Publisher>
          <ExtensionData />
          <Founded>1910</Founded>
          <Location>London</Location>
          <Name>Secker and Warburg</Name>
        </Publisher>
        <Title>Animal Farm</Title>
      </Book>
    </Books>
    

    As explained above, this xmlns="" can be eliminated by setting the Book element’s namespace (and that of its descendants) to correspond to the root’s. For the XmlSerializer class, the default namespace for all elements can be specified through the second parameter to its constructor. (The actual technique would vary depending on which serialization strategy you’re using.)

    using (var libraryClient = new LibraryServiceReference.LibraryServiceClient())
    {
        var book = libraryClient.GetBook();
    
        var stringBuilder = new StringBuilder();
        using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder))
        {
            xmlWriter.WriteStartElement("Books", "http://example.com/library");
            var serializer = new XmlSerializer(book.GetType(), "http://example.com/library");
            serializer.Serialize(xmlWriter, book);
            xmlWriter.WriteEndElement();
        }
    
        return stringBuilder.ToString();
    }
    

    And that would give the expected result:

    <?xml version="1.0" encoding="utf-16"?>
    <Books xmlns="http://example.com/library">
      <Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <ExtensionData />
        <Author>George Orwell</Author>
        <Publisher>
          <ExtensionData />
          <Founded>1910</Founded>
          <Location>London</Location>
          <Name>Secker and Warburg</Name>
        </Publisher>
        <Title>Animal Farm</Title>
      </Book>
    </Books>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that is generated in one class public class CreatingClass {
I have a domain object that has been auto generated for me by MyGeneration.
i have a collection of object. when i add some object on that collection.
I have a WCF service that I'd like clients to be able to reference
It's Friday refactor time!!! I have an object that I use to generate SQL
I have a Winforms application that generates its own PrintDocument object for printing. It
I have an XML document that I generate from an Entity Framework object. The
I have Written a webservice with VS2008 after I had Added Reference to that
I have a service contract that defines a method with a parameter of type
I have an object that implements IteratorAggregate and ArrayAccess , which internally contains an

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.