Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created an xml file called linksXML with the code
XML
<?xml version="1.0" encoding="utf-8"?>
<favorites>
	        <link url="http://www.google.com/" >Google/</link>
			<link url="http://www.codeproject.com/">CodeProject</link>
			<link url="http://www.youtube.com/edu">Youtube/Edu</link>
</favorites>


And I have this on my windows forms application

C#
private void addLink(String url, string name)
        {
            //open the xml file 
            XmlDocument myXml = new XmlDocument();
            //and a new element to the xml file
            XmlElement el = myXml.CreateElement("link");
            el.SetAttribute(b1.Text, url);
            el.InnerText = b2.Text;

            if (!File.Exists(linksXML))
            {
                XmlElement root = myXml.CreateElement("links");
                myXml.AppendChild(root);
                root.AppendChild(el);
            }
            else
            {
                myXml.Load(linksXML);
                myXml.DocumentElement.AppendChild(el);
            }
            //if the links bar is visible then 
            //you have to add a ToolStripButton
            if (linkBar.Visible == true)
            {
                //create a new ToolStripButton object with the favicon image,
                //website name the click eventhandler to 
                //navigate to the specific web site               
                ToolStripButton b =
                          new ToolStripButton(el.InnerText, getFavicon(url),
                          items_Click, el.GetAttribute("url"));
                b.ToolTipText = el.GetAttribute("url");
                //the MouseUp event is used 
                //for showing the context menu of this button 
                b.MouseUp += new MouseEventHandler(b_MouseUp);
                linkBar.Items.Add(b);
            }

            myXml.Save(linksXml);
        }


What I have tried:

This line gives me an error. Most of the places where I wrote linksXML shows a red line. I do not understand why.

if (!File.Exists(linksXML))
Posted
Updated 7-Oct-21 4:52am
v3
Comments
CHill60 7-Oct-21 7:39am    
What is the error?!
candijen 7-Oct-21 7:57am    
In reply to your solution below I added the Xmlfile to my solution explorer in visual studio, does it still need the path? and I did not understand what you meant by "class level variable called linksXML to which you have assigned a value in code that you have not shared"
CHill60 7-Oct-21 8:17am    
I've updated my solution

1 solution

If your file is called linksXML then it will also need a path and an extension so that line should read something like
C#
if (!File.Exists("c:\myfolder\linksXML,xml"))

The only way the code you have could work is if you have a class level variable called linksXML to which you have assigned a value in code that you have not shared

Edit after OP Comment:
By adding the XML file to your solution you are able to edit the file within Visual Studio. That does not mean you can reference it with just that part of it's name.

Look at the documentation for File.Exists(String) Method (System.IO) | Microsoft Docs[^]. It is expecting a string. The fact that Visual Studio is showing a red line under linksXML means that it does not know what linksXML is.

Either pass a string directly to the File.Exists function OR somewhere you need to declare linksXML as a variable and assign it a value e.g.
C#
string linksXML = "c:\myfolder\linksXML";
There is no declaration for linksXML in the code you have shared, so if you have declared a variable it was outside of the method addLink. For such a variable to be in scope it would have to have been declared within the class that contains addLink. See https://www.pluralsight.com/guides/understanding-scope-and-visibility-in-c[^] for more detail.

If you are including the XML file in your solution then yes, you still need the path and you will need to take care over which path is used depending on debug or release build - see How to get the current project path[^] for more details.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900