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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:04:43+00:00 2026-05-17T22:04:43+00:00

I am working with this XSD file. The portion of the XML that is

  • 0

I am working with this XSD file. The portion of the XML that is relevant to this question is here:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="https://wsmrc2vger.wsmr.army.mil/rcc/manuals/106-11" 
           targetNamespace="https://wsmrc2vger.wsmr.army.mil/rcc/manuals/106-11"
           elementFormDefault="qualified" 
           attributeFormDefault="unqualified">
    <xs:element name="Tmats">
        <xs:complexType>
            <xs:sequence>
                <xs:annotation>
                    <xs:documentation>TMATS G Group</xs:documentation>
                </xs:annotation>
                <xs:element name="ProgramName" type="xs:string" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>PN</xs:documentation>
                    </xs:annotation>
                </xs:element>

To get the documentation value for a given xs:element, I have this small function, which recursively walks the descendant nodes until it finds the documentation element:

public string GetCode(XElement e)
{
    foreach (var s in e.Elements())
    {
        // If we hit an intervening element, bail out.
        if (s.Name.ToString().Contains("element"))
            return "";

        if (s.Name.ToString().Contains("annotation"))
        {
            // I'll explain this loop in a moment.
            foreach (var t in s.Elements())
            {
                if (t.Name.ToString().Contains("documentation"))
                    return t.Value;
            }
        } 
        else
            return GetCode(s);
    }
    return "";
}

So far so good. The unit test looks like this:

[TestMethod()]
public void GetCodeTest()
{
    string path = @"C:\Documents and Settings\harvey robert\Desktop\Tmats.xsd";

    IEnumerable<XElement> elements =
        from e in XElement.Load(path).Elements()
        select e;

    TmatsDictionary target = new TmatsDictionary(); 
    XElement x = elements.First();
    string actual = target.GetCode(x);
    Assert.AreEqual("TMATS G Group", actual);
}

Which passes. Now I want to extend the test by adding an additional case, like this:

    XElement z = elements.DescendantsAndSelf()
                         .First(y => y.Attribute("name")
                         .ToString().Contains("ProgramName"));

    actual = target.GetCode(z);
    Assert.AreEqual("PN", actual);

…But this fails due to a null object reference (most likely y.Attribute("name")).

Did you see the loop in the function above that I commented?

// I'll explain this loop in a moment.
foreach (var t in s.Elements())
{
    if (t.Name.ToString().Contains("documentation"))
        return t.Value;
}

It’s written that way because I can’t figure out how to express the condition in a Lambda statement that works.

Any suggestions?

  • 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-17T22:04:44+00:00Added an answer on May 17, 2026 at 10:04 pm

    Here is the code that works.

    Notice the call to the GetName() method in the DescendantsAndSelf() call. This returns a properly formatted URI-prefixed name of the form {http://www.w3.org/2001/XMLSchema}element, which will match correctly to the xs:element names.

    The result is that DescendantsAndSelf() returns only those elements having the name xs:element, all of which have attributes associated with them (so there is no chance of a null reference error when referencing the Attributes collection).

    [TestMethod()]
    public void GetCodeTest()
    {
        string path = @"C:\TestArea\Tmats_09-2010.xml";
    
        IEnumerable<XElement> elements =
            from e in XElement.Load(path).Elements()
            select e;
    
        TmatsDictionary target = new TmatsDictionary();            
        XNamespace ns = "http://www.w3.org/2001/XMLSchema";
    
        XElement z = elements.DescendantsAndSelf(ns.GetName("element")) 
                             .First(y => y.Attribute("name")
                             .Value.Equals("ProgramName"));
    
        actual = target.GetCode(z);
        Assert.AreEqual("PN", actual);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is working: <?xml version=1.0 encoding=UTF-8?> <beans:beans xmlns=http://www.springframework.org/schema/security xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:beans=http://www.springframework.org/schema/beans xmlns:context=http://www.springframework.org/schema/context xsi:schemaLocation=http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context
this is the working version of my web.xml file: <?xml version=1.0 encoding=UTF-8?> <web-app xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
My 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 xsi:schemaLocation=http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd> <bean id=filterBySlic class=ca.ups.tundra.msg.FilterMessagesBySlic>
The top of my web.xml file looks like this: <?xml version=1.0 encoding=UTF-8?> <web-app xmlns=http://java.sun.com/xml/ns/j2ee
Data file: <?xml version=1.0 encoding=UTF-8 standalone=no?> <databaseChangeLog xmlns=http://www.liquibase.org/xml/ns/dbchangelog xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd> <changeSet > ...
I am working on getting an xml file to validate against an XSD schema
I do not get the last version of node-http-proxy working (this used to work
I am using xsd to generate classes from an xml file. It is working
I have an XSD file which contains the schema for my XML. The XSD
Source xml file: <?xml version=1.0 encoding=UTF-8 standalone=yes?> <!-- Inbound Message --> <Sales xmlns=abc:org.xcbl:schemas/xcbl/v4/xcbl4.xsd xmlns:SOAP-ENV=http://www.w3.org/2003/05/soap-envelope

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.