如何将XmlNodeList转换为Xmlstring以将xml数据与数据集绑定

时间:2013-01-25 09:44:47

标签: c# asp.net xml

XmlDocument objXmldoc = new XmlDocument();
objXmldoc.Load(txtBrowseFilePath.Text);    
XmlNodeList objxmlNodeList=null;
    objxmlNodeList = objXmldoc.SelectNodes(@"/AppXmlLogWritter/LogData[LogDateTime/text()[starts-with(. , '" + dateTimePickerFromDate.Value.ToString("yyyyMMdd") + "')]]");

DataSet ds = new DataSet();         
ds.ReadXml(objxmlNodeList);

如何将XmlNodeList转换为Xmlstring以将xml数据与数据集

绑定

xml文件

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<AppXmlLogWritter>
<LogData>
    <LogID>5678201301161640382918</LogID>
    <LogDateTime>20130116164037</LogDateTime>
    <LogType>Message</LogType>
    <LogFlag>RISLogFlag</LogFlag>
    <LogApplication>BaburaoLogApplication</LogApplication>
    <LogModule>RISLogModule</LogModule>
    <LogLocation>RISLogLocation</LogLocation>
    <LogText>BaburaoLogText</LogText>
  </LogData>
<LogData>
    <LogID>5678201301161640382919</LogID>
    <LogDateTime>20130116164038</LogDateTime>
    <LogType>Warning</LogType>
    <LogFlag>MACLogFlag</LogFlag>
    <LogApplication>MACLogApplication</LogApplication>
    <LogModule>MACLogModule</LogModule>
    <LogLocation>MACLogLocation</LogLocation>
    <LogText>MACLogText</LogText>
  </LogData>
</AppXmlLogWritter>

1 个答案:

答案 0 :(得分:2)

尝试这样的功能:

    private string XmlNodeListToString(XmlNodeList nodeList)
    {
        String returnStr = "";
        if (nodeList != null)
        {
            foreach (XmlNode node in nodeList)
            {
                returnStr += node.OuterXml;
            }

        }
        /*
        //I think this is not needed from Dataset.ReadXml
        returnStr = returnStr .Replace("&", "&amp;");
        returnStr = returnStr.Replace("<", "&lt;");
        returnStr = returnStr.Replace(">", "&gt;");
        returnStr = returnStr.Replace("'", "&apos;");
        returnStr = returnStr.Replace("\"", "&quot;");
        */


        return "<Root>"+returnStr+"</Root>";
    }

同样来自您的评论似乎您有一个长文件名问题。

尝试objXmldoc.Load(@txtBrowseFilePath.Text);

或者如果不成功 正如我在link

中找到的那样尝试使用\\?\之前的文件路径

例如,"\\?\D:\very long path"