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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:27:11+00:00 2026-05-13T07:27:11+00:00

I have a simple object I’m trying to serialize using DataContractSerializer. In my unit

  • 0

I have a simple object I’m trying to serialize using DataContractSerializer.

In my unit test, I’m trying to verify the xml contains the right value for the product/sku node.

My product class:

[DataContract(Namespace = "http://foo.com/catalogue/")
partial class Product
{
   [DataMember(Name = "sku")]
   public virtual string ProductSKU
   {
      get { return _productSKU; }
      set
      {
         OnProductSKUChanging();
        _productSKU = value;
        OnProductSKUChanged();
      }
   }
}

Here’s the method I’m testing:

public XDocument GetProductXML(Product product)
    {
        var serializer = new DataContractSerializer(typeof(Product));
        var document = new XDocument();

        using (var writer = document.CreateWriter())
        {
            serializer.WriteObject(writer, product);
            writer.Close();
        }

        return document;
    } 

And here’s my unit test:

[Test]
    public void Can_get_product_xml()
    {
        // Arrange
        var product = new Product {Id = 1, Name = "Foo Balls", ProductSKU = "Foo123"};
        var repository = new CatalogueRepository();
        var expectedProductSKU = "Foo123";

        // Act
        var xml = repository.GetProductXML(product);
        var actualProductSKU = xml.Element("product").Element("sku").Value;

        // Assert
        Assert.AreEqual(expectedProductSKU, actualProductSKU);
    }

The problem is that I get a Null reference when I try and access the xml elements, even though when I set a break point the var xml contains:

<product xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://foo.com catalogue/">
  <sku>Foo123</sku> 
</product>

Any ideas why this is not working? Do I need to parse the serialized stream before adding it to the XDocument?

  • 1 1 Answer
  • 1 View
  • 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-13T07:27:11+00:00Added an answer on May 13, 2026 at 7:27 am

    I would think the problem lies in the fact your XML document has a XML namespace xmlns="http://foo.com catalogue/", but when you select using Linq-to-XML, you’re never using that namespace in any way, shape or form.

    Also, the <product> tag is your XML root tag – you cannot reference that as an element.

    Try something like this:

    XNamespace ns = "http://foo.com/catalogue/";
    
    var root = xml.Root;
    var sku = root.Element(xns + "sku").Value;
    

    If you want to be sure, first assign .Element() to a variable and check for != null

    var sku = root.Element(xns + "sku");
    if(sku != null)
    {
       var skuValue = first.Value;
    }
    

    Hope that helps a bit !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple JSON object graph I'm trying to render using a Mustache
I have a simple java String object and wanna serialize/deserialize it, using XStream. Serialization
I have a simple Button control that contains an Image object as its content.
I have a simple XElement object XElement xml = new XElement(XML, new XElement (TOKEN,Session[Token]),
I have the following simple xml object file: [AuthorList] => SimpleXMLElement Object ( [@attributes]
I have a simple javascript object with several unknown properties containing a value. The
I have a simple unit test which tests that a validation function picks up
I have a simple object graph that I would like to serialize, I haven't
I have a simple object like this public class Test { public string Name
I have a simple application: object Test extends App { implicit def t2mapper[X, X0

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.