1

I am trying to store an XML file as a project resource so I do not have to hard code any file paths when I compile. I keep on getting an error when I call ReadXML. Any thoughts? If there are better ways of referencing a file without hard coding a path please let me know.

Thanks!

public class XMLLoad
{

    public DataSet ds { get; set; }
    public string PrimaryKey { get; set; }
    public string XLETable 
    {
        get
        {
            //Returns an XML file
            return Properties.Resources.mainXLETable;
        }
    }

    public XMLLoad(string xmlPrimaryKey)
    {
        this.PrimaryKey = xmlPrimaryKey;
    }

    public DataSet ReturnXMLFileAsDataSet(string dataTableName)
    {
        try
        {
            var reader = XmlReader.Create(XLETable);

            var dt = new DataTable(dataTableName);
            ds.ReadXml(reader);
            dt = ds.Tables[0];

            return ds;
        }
        catch (Exception)
        {
            throw;
        }   
    }
}
5
  • you do not want to physically access xml file???? Commented Nov 22, 2012 at 19:53
  • Yeah it would be OK if I could put in in the solution to physically access it. My problem is that I do not want to break the file path references when I build and deploy the application. Commented Nov 22, 2012 at 20:00
  • if it in the solution it will always reference the same place what I do not understand is why you want to write it in a dataset Commented Nov 22, 2012 at 20:01
  • That is what I can't figure out. For whatever reason when I hard codes the relative path to the xml file it can't locate it. I have it at the root of my solution. Commented Nov 22, 2012 at 20:09
  • 1
    It should be just by using the mappath msdn.microsoft.com/en-us/library/… Commented Nov 22, 2012 at 21:20

1 Answer 1

2

The Create method is expecting an URL not a string containing the XML data.

Try this:

var reader = XmlReader.Create(new StringReader(XLETable));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.