开发者

C# XML - Reading Included XML Document

开发者 https://www.devze.com 2023-01-28 12:51 出处:网络
I have created a basic project in to which i have added a reall开发者_开发百科y simple xml file. I can see the file and my one form in the project solution. I am trying to write code to read the xml f

I have created a basic project in to which i have added a reall开发者_开发百科y simple xml file. I can see the file and my one form in the project solution. I am trying to write code to read the xml file but i cant seem to access it as visual studio doesnt seem to be picking up that its there if that makes sense? How do i get at that file so i can do something like

XmlDocument doc = new XmlDocument();
doc.Load("My document here")

Thanks


You mean you added the xml-file to the solution? If so, you need to edit the properties of the xml-file (right click in solution-explorer and select properties), and set it to always copy on build. This will copy the xml-file to the bin/Debug-folder when you build, and this is where the program is run from. If not you need to change the file-path to "../../filename.xml", this will also work.

Note:
This is only a solution to your problem if the xml-file is part of the solution, and the program doesn't find the file when you run it. And you must not be using absolute paths.


Add the xml file as a resource to your project (build action = embedded resource). And use:

    public static XmlDocument GetEmbeddedXml(Assembly assembly, string fileName)
    {
        using (var str = GetEmbeddedFile(assembly, fileName))
        {
            using (var tr = new XmlTextReader(str))
            {
                var xml = new XmlDocument();
                xml.Load(tr);
                return xml;
            }
        }
    }

    public static Stream GetEmbeddedFile(Assembly assembly, string fileName)
    {
        string assemblyName = assembly.GetName().Name;
        Assembly a = Assembly.Load(assemblyName);
        Stream str = a.GetManifestResourceStream(assemblyName + "." + fileName);

        if (str == null)
            throw new Exception("Could not locate embedded resource '" + fileName + "' in assembly '" + assemblyName + "'");
        return str;

    }


You need to do something along the lines of:

using(XmlTextReader reader = new XmlTextReader ("yourfile.xml"))
{

    while (reader.Read()) 
    {
        switch (reader.NodeType) 
        {
            case XmlNodeType.Element: // The node is an element.
                Console.Write("<" + reader.Name);
                Console.WriteLine(">");
                break;
            case XmlNodeType.Text: //Display the text in each element.
                Console.WriteLine (reader.Value);
                break;
            case XmlNodeType. EndElement: //Display the end of the element.
                Console.Write("</" + reader.Name);
                Console.WriteLine(">");
                break;
        }
    }
}

I assume you want to parse the XML file after you have read it in?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号