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

  • Home
  • SEARCH
  • 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 9179835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:53:59+00:00 2026-06-17T17:53:59+00:00

I’m loading an XML file, getting the values of some nodes and then setting

  • 0

I’m loading an XML file, getting the values of some nodes and then setting those values into an object.

The problem is that although I can get into the each node, I can’t get its value: it just returns null. Searching in Google and SO I found several threads saying that usually it is a problem of namespaces. So I set up a namespace on the XML file and in my code to work with it. This is now when things get funny:

  • If all the nodes but the last one are prefixed (in my code) with the namespace, all of them gets set except that particular one which gives an ArgumentNullException (tabla does not accept nulls on some properties). This seems to be expected behavior.
  • if I correct that last line and set up the namespace (as it should be), then the exception gets raised at the FIRST node.

This is the XML:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://daniel.ponticelli.com/ns/dataExport">
  <PollInstance>
    <Poll_Instance_id>1</Poll_Instance_id>
    <POLL_description>Mes 1</POLL_description>
    <dt_start>01/09/2012 12:00:00 a.m.</dt_start>
    <dt_end>30/09/2012 12:00:00 a.m.</dt_end>
    <POLL_status>0</POLL_status>
    <dt_created>03/09/2012 09:50:36 a.m.</dt_created>
    <dt_updated>03/09/2012 09:50:36 a.m.</dt_updated>
    <id_original_clone>0</id_original_clone>
    <target_locs>0</target_locs>
    <id_poll>1</id_poll>
  </PollInstance>
</root>

This is the “wrong” code that has the last element without namespace:

XElement instancia = xml.Descendants().First();
XNamespace ra = "http://daniel.ponticelli.com/ns/dataExport";

tabla.Poll_Instance_id = (int)instancia.Element(ra + "Poll_Instance_id");
tabla.POLL_description = (string)instancia.Element(ra + "POLL_description");
tabla.dt_start = DateTime.Parse((string)instancia.Element(ra + "dt_start"));
tabla.dt_end = DateTime.Parse((string)instancia.Element(ra + "dt_end"));
tabla.POLL_status = (int)instancia.Element(ra + "POLL_status");
tabla.dt_created = DateTime.Parse((string)instancia.Element(ra + "dt_created"));
tabla.dt_updated = DateTime.Parse((string)instancia.Element(ra + "dt_updated"));
tabla.id_original_clone = (int)instancia.Element(ra + "id_original_clone");
tabla.target_locs = (int)instancia.Element(ra + "target_locs");
tabla.id_poll = (int)instancia.Element("id_poll");

The previous code gives the exception on the last line.

Now this is the “correct” code that has all the elements prefixed with the namespace:

XElement instancia = xml.Descendants().First();
XNamespace ra = "http://daniel.ponticelli.com/ns/dataExport";

tabla.Poll_Instance_id = (int)instancia.Element(ra + "Poll_Instance_id");
tabla.POLL_description = (string)instancia.Element(ra + "POLL_description");
tabla.dt_start = DateTime.Parse((string)instancia.Element(ra + "dt_start"));
tabla.dt_end = DateTime.Parse((string)instancia.Element(ra + "dt_end"));
tabla.POLL_status = (int)instancia.Element(ra + "POLL_status");
tabla.dt_created = DateTime.Parse((string)instancia.Element(ra + "dt_created"));
tabla.dt_updated = DateTime.Parse((string)instancia.Element(ra + "dt_updated"));
tabla.id_original_clone = (int)instancia.Element(ra + "id_original_clone");
tabla.target_locs = (int)instancia.Element(ra + "target_locs");
tabla.id_poll = (int)instancia.Element(ra + "id_poll");

This code now gives the exception on the third line! (the one that begins with tabla.Poll_Instance_id). If I set a breakpoint on the “wrong” code just before the last line, I can check in VS that indeed the values are being retrieved from the XML and set on the object tabla as expected; so I don’t know what’s going on.

  • 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-06-17T17:54:02+00:00Added an answer on June 17, 2026 at 5:54 pm

    I have found the solution, thanks to @lazyberezovsky for pointing me to the right direction with the root() thing.

    The problem was on the Descendants() method, which gives me all the descendant nodes; so I was getting the PollInstance node and also the Poll_instance_id and the others. That’s why it breaks on the first line in the “right” code: after processing the first node, it choked on the others.

    Instead I have to use the Elements() method which returns only the direct childs; in my case, only the PollInstance.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from

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.