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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:50:28+00:00 2026-05-26T05:50:28+00:00

I have a XML file like this: <Document> <Tests Count=4> <Test> <Name>test with a</Name>

  • 0

I have a XML file like this:

   <Document>
       <Tests Count="4">
          <Test>
             <Name>test with a</Name>
             <Type>VI test</Type>
             <Result>Pass</Result>
          </Test>
          <Test>
             <Name>test plot</Name>
             <Type>Curve test</Type>
             <Result>Fail</Result>
          </Test>
          <Test>
             <Name>test fixture</Name>
             <Type>Leakage test</Type>
             <Result>Pass</Result>
          </Test>      
          <Test>
             <Name>test fixture</Name>
             <Type>Leakage test</Type>
          </Test>             
        </Tests>
   </Document>

I made a class to contain each “Test” in it:

class TestGroup
{
    public string TestName { get; set; }
    public string TestType { get; set; }
    public string TestResult { get; set; }
}

Since I am very noob at XML, at the moment I get data like this (ztr is XmlDocument):

public List<TestGroup> GetTestGroups()
        {
            List<TestGroup> TestNode = new List<TestGroup>();

            string[] type_t = new string[GetTestsNumber()];
            string[] name_t = new string[GetTestsNumber()];
            string[] result_t = new string[GetTestsNumber()];

            //name
            string xpath = "/Document/Tests/Test";

            int i = 0;

            foreach (XmlElement Name in ztr.SelectNodes(xpath))
            {
                name_t[i] = Name.SelectSingleNode("Name").InnerText;
                i++;
            }
            ///////////

            //result
            xpath = "/Document/Tests/Test";

            i = 0;

            foreach (XmlElement Result in ztr.SelectNodes(xpath))
            {
                result_t[i] = Result.SelectSingleNode("Result").InnerText;
                i++;
            }
            /////////////////
            xpath = "/Document/Tests/Test";

            i = 0;

            foreach (XmlElement Type in ztr.SelectNodes(xpath))
            {
                type_t[i] = Type .SelectSingleNode("Type").InnerText;
                i++;
            }

            int count = type_t.GetLength(0);

            for (i = 0; i < count; i++)
            {
                LUTestGroup node = new LUTestGroup();
                node.TestName = name_t[i];
                node.TestType = type_t[i];
                node.TestResult = result_t[i];

                TestNode.Add(node);
            }

            return TestNode;
        }

Obviously this way is pretty dangerous. It itterates separate times in the xml time and each time adds one property to the class. Also if you note in the example xml file. the last Test does noet have a Result node so the code I wrote sometimes crashes when I feed in different XML files.

Can you help me please write a method to safely get the List to be filled by all Tests avilable in XML file and if any of them does not have a node, the method would not crash? and must important, does the job in ONE GO!

Thanks

  • 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-26T05:50:29+00:00Added an answer on May 26, 2026 at 5:50 am

    (As noted in comments.)

    The LINQ to XML version is really easy:

    // Could use doc.Root.Element("Tests").Elements("Test") to be explicit
    return doc.Descendants("Test")
              .Select(x => new LUTestGroup {
                          TestName = (string) x.Element("Name"),
                          TestType = (string) x.Element("Type"),
                          TestResult = (string) x.Element("Result")
                      })
              .ToList();
    

    Note that the cast from XElement to string will return null if you give it a null XElement reference, so in your “leakage test” case, you’d end up with an LUTestGroup with a null TestResult property.

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

Sidebar

Related Questions

I have a XML file like this: <Document> <Tests> <Test> <Name>A</Name> <Value>0.01</Value> <Result>Pass</Result> </Test>
I have a xml file like this <Document> <Tests> <Test> <Name>A</Name> <Type>Like A</Type> <Members>
I have a XML file like this: <Document> <Tests> <Test> <Name>A</Name> <Value>1</Value> </Test> <Test>
I have an xml file like this: <result> <customer> <id>1</id> <name>A</name> </customer> <customer> <id>2</id>
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>
I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
I have an XML file formatted like this: <?xml version=1.0 encoding=utf-8?> <Snippets> <Snippet name=abc>
Suppose i have an xml document like this XML File: <document> <educationalsection> educational details
I have an XML file like this: <SiteConfig> <Sites> <Site Identifier=a /> <Site Identifier=b
I have an XML file looks like this: <?xml version=1.0 encoding=utf-8 ?> <PathMasks> <Mask

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.