Question

I have to readout an XML-file (with already existing content) from my local Visual Studio 2013 project structure, but not from harddrive-directory (as mentioned in the most tutorials/ guides, I've read in the last 2 hours...). After the readout process, I have to search for some tag-names in it.

I have to do this way, because the requirement is, that the specified local XML-file shouldn't appear after the build process in output directory...

So how can I do that?

==================================================================================

This is my AccessData.xml-file (In Project-directory: "LinqToXML_Example/AccessData.xml"):

<?xml version="1.0" encoding="utf-8"?>

  <Clients>


    <Client>

        <Username>Administrator</Username>

        <Password>Admin-Password</Password>

        <Settings>
          <Item1>Admin-Setting 1</Item1>
          <Item2>Admin-Setting 2</Item2>
          <Item3>Admin-Setting 3</Item3>
        </Settings>

    </Client>



    <Client>

        <Username>Service</Username>

        <Password>Srv-Password</Password>

        <Settings>
          <Item1>Srv-Setting 1</Item1>
          <Item2>Srv-Setting 2</Item2>
          <Item3>Srv-Setting 3</Item3>
        </Settings>

    </Client>



    <Client>

        <Username>Customer</Username>

        <Password>Cust-Password</Password>

        <Settings>
          <Item1>Cust-Setting 1</Item1>
          <Item2>Cust-Setting 2</Item2>
          <Item3>Cust-Setting 3</Item3>
        </Settings>

    </Client>


  </Clients>

This is my Program.cs-file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Reflection;

namespace LinqToXML_Example
{
    public class Program
    {


        public static string SearchForUsername(string username, XElement clients)
        {

            var userName = from p in clients.Elements()
                            where p.Element("Username").Value == username
                            select p.Element("Username").Value;

            foreach (var p in userName)
            {
                return p + " found!";
            }

            return "User '" + username + "' does not exist!";
        }



        public static bool IsUsernameExisting(string username, XElement clients)
        {
            var userName = from p in clients.Elements()
                            where p.Element("Username").Value == username
                            select p.Element("Username").Value;

            foreach (var p in userName)
            {
                return true;
            }

            return false;
        }



        public static string SearchForUsernamePassword(string username, XElement clients)
        {

            if (IsUsernameExisting(username, clients))
            {
                var password = from p in clients.Elements()
                                where p.Element("Username").Value == username
                                select p.Element("Password").Value;

                foreach (var p in password)
                    return "The password of client " + username + " is: " + p;
            }

            return "Cannot get the username's password, because of a wrong Username!";
        }



        public static List<string> ReadUserSettings(string username, XElement clients)
        {

            List<string> settingsList = new List<string>();


            if (IsUsernameExisting(username, clients))
            {
                var setting = from s in clients.Elements()
                                where s.Element("Username").Value == username
                                select s.Element("Settings");

                foreach (var p in setting)
                {
                    settingsList.Add(p.Element("Item1").Value);
                    settingsList.Add(p.Element("Item2").Value);
                    settingsList.Add(p.Element("Item3").Value);
                }

                return settingsList;
            }


            var errorMsg = "Cannot get the username's settings, because of a wrong Username!";
            settingsList.Add(errorMsg);

            return settingsList;
        }





        public static void Query(string username, XElement clients)
        {
            // Search for specific Username:
            Console.WriteLine("Search for Client " + "'" + username + "' (based on Usernames):");

            string result = SearchForUsername(username, clients);
            Console.WriteLine(result);


            Console.WriteLine("");


            // Search for Password of Client:
            Console.WriteLine("Search for Password of Client " + "'" + username + "':");

            result = SearchForUsernamePassword(username, clients);
            Console.WriteLine(result);


            Console.WriteLine("");


            // Readout the Settings of Client:
            Console.WriteLine("Readout the Settings of Client " + "'" + username + "':");

            List<string> resultList = new List<string>();
            resultList = ReadUserSettings(username, clients);

            if (resultList.Count != 1)
            {
                for (int i = 0; i < resultList.Count(); i++)
                {
                    var itemcounter = i + 1;
                    Console.WriteLine("Item" + itemcounter + ": " + resultList.ElementAt(i));
                }
            }

            else Console.WriteLine(resultList.ElementAt(0));



            Console.Read();
        }




        static void Main()
        {

            var asm = Assembly.GetExecutingAssembly();

  // Bug fix: "AccessData.xml" has to be changed to: "LinqToXML_Example.AccessData.xml" 
            var textStream = asm.GetManifestResourceStream("AccessData.xml");

       // Bug fix: Has to be changed to: var xmlReader = new XmlTextReader(textStream);
            var xmlReader = new XmlReader(textStream);

            XElement clients = XElement.Load(xmlReader);

            Query("Administrator", clients);
        }
    }
}

I've changed the properties of AccessData.xml file to:

Build Action = "Resource" // Bug fix: Has to be changed to: 'Embeded Resource'

Copy to output directory = "Never"

Unfortunately I have no access to my xml-file :((

Was it helpful?

Solution

Having scanned your code roughly, the following line seems to be the problem:

var textStream = asm.GetManifestResourceStream("AccessData.xml");

By default, resource names are prefixed with the project's default namespace. Also, the project folders that the file is contained in are included in the name. So in your case, it is likely that the resource name is "LinqToXML_Example.AccessData.xml" instead of "AccessData.xml".

Also, you should set the build action to Embedded Resource.

If you want to find all the resource names that are included in your project file, you can use this code and check the return value when debugging:

string[] names = asm.GetManifestResourceNames();

See this link for details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top