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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:33:26+00:00 2026-05-26T14:33:26+00:00

I got a xml structure as below: <Users> <User Code=1 Roles=1,2,3 /> </Users> I

  • 0

I got a xml structure as below:

<Users>
<User Code="1" Roles="1,2,3" />
</Users>

I provide a method to search the xml file for retrieving particular user based on code like below

    string xpath = "Users/User[@Code="+ Code +"]";
    XmlNode user = _xmlDatabase.SelectSingleNode(xpath);
    if (user != null)
    {
        XmlAttributeCollection userMeta = user.Attributes;
        if (userMeta != null)
        {
            int code = int.Parse(Code);
            User userInstance = new User(Code, userMeta[1].Value, userMeta[2].Value);
            return userInstance;
        }
    }

i would invoke the method like so
User user = GetUserByCode("1"); & _xmlDatabase is a instance of XmlDocument class. Here is the question,

  • I get to return null when no matching user is found
  • Attributes i search for does not exists
  • It’s a fresh file

Hence i modified the method to return "null"only to be complained by compiler that "return statement is missing"

I kind of wanted the end-user to do

User user = GetUserByCode("1");
if(user == null)
  Display "No User Found"
  • 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-26T14:33:26+00:00Added an answer on May 26, 2026 at 2:33 pm

    please see the comments on below code

       if (user != null) // if user == null nothing will return 
        {
            XmlAttributeCollection userMeta = user.Attributes;
            if (userMeta != null) // if userMeta == null nothing will return 
            {
                int code = int.Parse(Code);
                User userInstance = new User(Code, userMeta[1].Value, userMeta[2].Value);
                return userInstance;
            }
        }
    

    you can solve this as below

    public User GetUserByCode(string Code)
    {
        User userInstance = null;
        string xpath = "Users/User[@Code="+ Code +"]";
        XmlNode user = _xmlDatabase.SelectSingleNode(xpath);
        if (user != null)
        {
            XmlAttributeCollection userMeta = user.Attributes;
            if (userMeta != null)
            {
                int code = int.Parse(Code);
                userInstance = new User(Code, userMeta[1].Value, userMeta[2].Value);
            }
        }
    
        return userInstance;
    }
    

    Above code will return null or userInstance in any case.

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

Sidebar

Related Questions

I've got to build an XSD file for XML structure verifying purposes, as usual.
I've got an xml file with such structure: <panel> <tr> <td> <element> ... smth
I got a row structure looks like ID bigint, ScanRept XML I want to
I've got an XML file that contains a list of questions. I'd like to
I've got a XML Schema (xsd file) snippet bellow, in which I want the
I've got broken data.xml file, how i can fix it with php? I need
I have an XML file in the following structure. <NewDataSet> <markers> <name>name text</name> <desc>desc
i got a XML File as export from Wireshark and want to select the
I've got a fairly big XML file that I need to parse into a
I've got following XML structure: <artists> <artist> <name></name> <image size=small></image> <image size=big></image> </artist> </artists>

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.