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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:52:38+00:00 2026-05-15T21:52:38+00:00

I have an XML structure like the following: <Root> <Base> <Greeting>Base Hello!</Greeting> <SpecialMessage>Base Special

  • 0

I have an XML structure like the following:

<Root>
    <Base>
        <Greeting>Base Hello!</Greeting>
        <SpecialMessage>Base Special Message</SpecialMessage>

        <Products>
            <Product id="001">
                <Greeting>Base 001 Hello!</Greeting>
            </Product>
        </Products>
    </Base>

    <Buy>
        <Greeting>Buy Hello!</Greeting>
        <SpecialMessage>Buy Special Message</SpecialMessage>
        <Products>
            <Product id="001">
                <Greeting>Buy 001 Hello!</Greeting>
            </Product>
        </Products>
    </Buy>

    <Rent>
        <Greeting>Rent Hello</Greeting>
    </Rent>
</Root>

The Base, Buy and Rent nodes will always exist, but the nodes within them may or may not. At runtime, I’ll determine if this is a Buy or Rent scenario. I also may or may not have a product id.

For example, if it’s a buy scenario and the product id is “001”, I’ll get the “Buy 001 Hello” Message. However, if the Product section in the Buy section were to be missing for whatever reason, I’ll return “Base 001 Hello”.

A second example would be if I wanted to get the SpecialMessage for a Rent scenario. Since no SpecialMessage node is in the Rent section, I’ll return “Base Special Message”

I assemble a list of XPaths like this, using Greeting as an example in a Buy scenario for product 001):

  1. /Root/Buy/Products/Product[id=001]/Greeting
  2. /Root/Base/Products/Product[id=001]/Greeting
  3. /Root/Buy/Greeting
  4. /Root/Base/Greeting

At the moment, I start at the top of the list, run the XPath, return the result if it’s found, other wise move on to the next XPath and so on:

string result;
result = RunXPath(xpaths["Level1"], document);
if (!String.IsNullOrEmpty(result))
    return result;

result = RunXPath(xpaths["Level2"], document);
if (!String.IsNullOrEmpty(result))
    return result;

result = RunXPath(xpaths["Level3"], document);
if (!String.IsNullOrEmpty(result))
    return result;

result = RunXPath(xpaths["Level4"], document);
if (!String.IsNullOrEmpty(result))
    return result;

return String.Empty;

While it works, it seems awkward and a little rough to read. Are there better approaches, in terms of readability?

  • 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-15T21:52:39+00:00Added an answer on May 15, 2026 at 9:52 pm

    One option is to create a collection of paths, and iterate over it:

    string[] paths = {"Level1", "Level2", "Level3", "Level4"};
    foreach(string path in paths)
    {
        result = RunXPath(xpaths[path], document);
        if (!String.IsNullOrEmpty(result))
            return result;
    }
    return "";
    

    Generally, if you find yourself write repeated code, you should consider using a loop or a method (or both!).
    Also, try not to use the same variable for different things. Go for meaningful names – it will help readability, and help detect errors. This is less offending in your senarion, but important enough. Consider:

    result = RunXPath(xpaths["/document/my:things/rooms[0]/floor"], document);
    Print(result);
    result = RunXPath(xpaths["/document/my:things/roomate/phone/mobile"], document);
    Print(result);
    

    I recently had to debug code exactly like that, and refactored it to:

    string floor = RunXPath(xpaths["/document/my:things/rooms[0]/floor"], document);
    Print(floor);
    string friendMobilePhone = RunXPath(xpaths["/document/my:things/roomate/phone/mobile"], document);
    Print(friendMobilePhone);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a XML Structure that looks like this. <sales> <item name=Games sku=MIC28306200 iCat=28
I have the following XML structure: <?xml version=1.0 ?> <course xml:lang=nl> <body> <item id=787900813228567
I have the following sample XML structure: <SavingAccounts> <SavingAccount> <ServiceOnline>yes</ServiceOnline> <ServiceViaPhone>no</ServiceViaPhone> </SavingAccount> <SavingAccount> <ServiceOnline>no</ServiceOnline>
If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
I have a huge bunch of XML files with the following structure: <Stuff1> <Content>someContent</name>
i have the following xml structure and want to add it to a treeview.
ich have a xml file with the following structure: <layer1 name=this is layer1> <messages>
Here's my problem.I have 2 xmlfiles with identical structure, with the second xml containing
I have a piece of XML that is structured similar to this: <root> <score
I have xml where some of the element values are unicode characters. Is it

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.