0

I'm trying to read some xml files which I have included in the Resources folder under my project. Below is my code:

public void ReadXMLFile(int TFType)
{
        XmlTextReader reader = null;

        if (TFType == 1)
            reader = new XmlTextReader(MyProject.Properties.Resources.ID01);
        else if (TFType == 2)
            reader = new XmlTextReader(MyProject.Properties.Resources.ID02);


        while (reader.Read())
        {
            if (reader.IsStartElement())
            {
                switch (reader.Name)
                {
                    case "Number":
                   // more coding on the cases.
}

But when I compile, there's an error on "QP2020E.Properties.Resources.ID01" saying: 'Illegal characters in path.' Do you guys know what's wrong?

randoms
  • 2,793
  • 1
  • 31
  • 48
Coolguy
  • 2,225
  • 12
  • 54
  • 81
  • Where did you get 'QP2020E.Properties.Resources.ID01' from? Is that the name of your project and resource? – Amr Sep 26 '12 at 06:41
  • Check what QP2020E.Properties.Resources.ID01 is returning. I do not think it will be a path and the XmlTextReader will need a path to a file. – TimVK Sep 26 '12 at 06:44
  • Yes. QP2020E is the project name. I've changed to MyProject for general now. – Coolguy Sep 26 '12 at 06:48
  • Yes Amr. I've put the ID01.xml file into the Resources folder under my project. Is that the correct way to call it? – Coolguy Sep 26 '12 at 06:50
  • See my answer below. There's a similar question here http://stackoverflow.com/questions/337535/reading-xml-file-from-a-resource – Amr Sep 26 '12 at 07:04

3 Answers3

1

The XmlTextReader constructor requires either a stream or a string. The one that requires a string is expecting a url (or path). You are passing it the value of your resource. You'll need to convert the string value into a stream.

To do this Wrap it in a StringReader(...)

reader = new XmlTextReader(new StringReader(MyProject.Properties.Resources.ID02)); 
Scott Baldwin
  • 431
  • 2
  • 10
  • I don't understand what you mean Scott Baldwin. Could you give me an example? Is it "reader = StringReader(MyProject.Properties.Resources.ID01)"? – Coolguy Sep 26 '12 at 07:15
  • Scott Baldwin, I've tried your answer. If I use your answer, then there's error on the line "reader.Read()" saying: 'Data at the root level is invalid. Line 1, position 1.' What's wrong? – Coolguy Sep 26 '12 at 07:58
  • It looks like you have progressed to the next stage of your problem. You are now successfully reading the xml, there is just some problem with the XML you are reading. this post might help [link](http://stackoverflow.com/questions/310669/why-does-c-sharp-xmldocument-loadxmlstring-fail-when-an-xml-header-is-included) – Scott Baldwin Sep 26 '12 at 08:08
1

You should provide the XMLTextReader with the file path not the file content. For instance, change

reader = new XmlTextReader(MyProject.Properties.Resources.ID01);

To:

StringReader s = new StringReader(MyProject.Properties.Resources.XmlFile);
XmlTextReader r = new XmlTextReader(s);
will-hart
  • 3,742
  • 2
  • 38
  • 48
Diaz
  • 146
  • 2
  • 8
0

To read an XML file from a resource, use XDocument.Parse as described in this answer

I think you need to modify your code to be like this:

public void ReadXMLFile(int TFType)
{
        XDocument doc = null;

        if (TFType == 1)
            doc = XDocument.Parse(MyProject.Properties.Resources.ID01);
        else if (TFType == 2)
            doc = XDocument.Parse(MyProject.Properties.Resources.ID02);


        // Now use 'doc' as an XDocument object
}

More info on XDocument is here.

Community
  • 1
  • 1
Amr
  • 1,935
  • 2
  • 19
  • 29