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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:45:54+00:00 2026-06-17T19:45:54+00:00

Given the following xml: <root xmlns=http://tempuri.org/myxsd.xsd> <node name=mynode value=myvalue /> </root> And given the

  • 0

Given the following xml:

<root xmlns="http://tempuri.org/myxsd.xsd">
    <node name="mynode" value="myvalue" />
</root>

And given the following code:

string file = "myfile.xml";  //Contains the xml from above

XDocument document = XDocument.Load(file);
XElement root = document.Element("root");

if (root == null)
{
    throw new FormatException("Couldn't find root element 'parameters'.");
}

If the root element contains the xmlns attribute then the variable root is null. If I remove the xmlns attribute then root is NOT null.

Can anyone explain why this is?

  • 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-17T19:45:55+00:00Added an answer on June 17, 2026 at 7:45 pm

    When you declare your root element like <root xmlns="http://tempuri.org/myxsd.xsd"> this means that your root element all of its descendants are in http://tempuri.org/myxsd.xsd namespace. By default namespace of an element has an empty namespace and XDocument.Element looks for elements without namespace. If you want to access an element with a namespace you should explicitly specify the namespace.

    var xdoc = XDocument.Parse(
    "<root>" +
        "<child0><child01>Value0</child01></child0>" +
        "<child1 xmlns=\"http://www.namespace1.com\"><child11>Value1</child11></child1>" +
        "<ns2:child2 xmlns:ns2=\"http://www.namespace2.com\"><child21>Value2</child21></ns2:child2>" +
    "</root>");
    
    var ns1 = XNamespace.Get("http://www.namespace1.com");
    var ns2 = XNamespace.Get("http://www.namespace2.com");
    
    Console.WriteLine(xdoc.Element("root")
                          .Element("child0")
                          .Element("child01").Value); // Value0
    
    Console.WriteLine(xdoc.Element("root")
                          .Element(ns1 + "child1")
                          .Element(ns1 + "child11").Value); // Value1
    
    Console.WriteLine(xdoc.Element("root")
                          .Element(ns2 + "child2")
                          .Element("child21").Value); // Value2
    

    For your case

    var ns = XNamespace.Get("http://tempuri.org/myxsd.xsd");
    xdoc.Element(ns + "root").Element(ns + "node").Attribute("name")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following XML <?xml version=1.0?> <Message> <ArrayOfStock xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema> <Stock> <StockID>9cddb639-25ee-4415-be07-3109e5ae9883</StockID> <Description>Stock Item
Given the following XML markup: <root xmlns=Demo> <child name=foo/> </root> and an XPathNavigator positioned
I have the following xsl stylesheet: <xsl:stylesheet xmlns=http://www.w3.org/1999/xhtml xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:output method=xml encoding=utf-8/> <xsl:template match=/>
Why does the following not work as I'd expect it to? <root xmlns:ns0=xmlns ns0:xsi=http://www.w3.org/2001/XMLSchema-instance
Given the following XML file: <?xml version=1.0 encoding=utf-8?> <Wix xmlns=http://schemas.microsoft.com/wix/2006/wi> <Fragment> <DirectoryRef Id=INSTALLLOCATIONAGENT> <Component
I have following XML file: <?xml version=1.0 encoding=UTF-8?> <beans xmlns=http://www.springframework.org/schema/beans xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:jaxrs=http://cxf.apache.org/jaxrs xmlns:context=http://www.springframework.org/schema/context xmlns:cxf=http://cxf.apache.org/core
Given the following XML: <root> <StepFusionSet name=SF1> <StepFusionSet name=SF2> </StepFusionSet> </StepFusionSet> <StepFusionSet name=SF10> </StepFusionSet>
Given the following XML structure: <root> <data>x</data> <details> <some_other_element>...</some_other_element> <collection> <element><a>1</a></element> <element><a>2</a></element> <element><a>3</a></element> <collection>
Given the following XML: <root> Pacman <format bold=1 italic=1>rules</format>! </root> What is a better
Given the following XML: <Contact> <ContactID>41111-f15a-4fa1-b643-47877608f557</ContactID> <ContactStatus>ACTIVE</ContactStatus> <Name>ABC Ltd</Name> <EmailAddress>xxx@xxx.co</EmailAddress> <SkypeUserName>xxxdemo</SkypeUserName> <Addresses> <Address> <AddressType>STREET</AddressType>

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.