简体   繁体   English

访问 XML 文件作为资源并加载到数据表

[英]Access XML file as resource and load to datatable

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.我正在尝试将 XML 文件存储为项目资源,因此在编译时不必对任何文件路径进行硬编码。 I keep on getting an error when I call ReadXML.当我调用 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;
        }   
    }
}

The Create method is expecting an URL not a string containing the XML data. Create方法需要一个 URL,而不是包含 XML 数据的字符串。

Try this:尝试这个:

var reader = XmlReader.Create(new StringReader(XLETable));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM