在 C# 中解析 XML 时路径错误中的非法字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1374729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:48:53  来源:igfitidea点击:

Illegal characters in path error while parsing XML in C#

c#xmlvisual-studio-2008

提问by Sathyajith Bhat

I'm getting an "Illegal characters in path error" while using XMLTextReader method. Basically, I'm sending a long URL to tr.im, and tr.im sends the response as an XML stream, which I'm trying to parse but I get the above mentioned error. Can you guys guide me as to why I'm getting this error and where I'm going wrong? Here's the code:

使用 XMLTextReader 方法时出现“路径错误中的非法字符”。基本上,我正在向 tr.im 发送一个长 URL,而 tr.im 将响应作为 XML 流发送,我正在尝试解析它,但出现上述错误。你们能指导我为什么我会收到这个错误以及我哪里出错了吗?这是代码:

WebRequest wrURL;
Stream objStream;
string strURL;
wrURL = WebRequest.Create("http://api.tr.im/api/trim_url.xml?url=" + HttpUtility.UrlEncode(txtURL.Text));
objStream = wrURL.GetResponse().GetResponseStream();
StreamReader objSReader = new StreamReader(objStream);
strURL = objSReader.ReadToEnd().ToString();
XmlTextReader reader = new XmlTextReader(strURL); //getting the error at this point

I'm using Visual Studio 2008, Express Edition

我使用的是 Visual Studio 2008 速成版

采纳答案by JaredPar

The reason why is you are using the constructor of XmlTextReader which takes a file path as the parameter but you're passing XML content instead.

原因是您使用 XmlTextReader 的构造函数,它将文件路径作为参数,但您传递的是 XML 内容。

Try the following code

试试下面的代码

XmlTextReader reader = new XmlTextReader(new StringReader(strURL));

回答by JSB????

You should print or otherwise display strUrl. Once you can actually see the path that you're passing to the test reader, it should be obvious what the path error is.

您应该打印或以其他方式显示strUrl. 一旦您可以实际看到您传递给测试读取器的路径,就应该很明显路径错误是什么。

Also, just looking at the code, it seems like the response itself might be XML, in which case you should pass objSReaderdirectly to the XmlTextReader constructor.

此外,仅查看代码,似乎响应本身可能是 XML,在这种情况下,您应该objSReader直接传递给 XmlTextReader 构造函数。

回答by dtb

The XmlTextReader(string)constructor expects a file path, not the actual XML data.

XmlTextReader(string)构造函数需要一个文件路径,而不是实际的XML数据。

You can create an XML reader directly from the stream. The recommended way to do this is using the XmlReader.Createmethod:

您可以直接从流创建 XML 阅读器。推荐的方法是使用XmlReader.Create方法:

XmlReader reader = XmlReader.Create(objStream);

回答by Darin Dimitrov

XmlTextReader constructor accepts a string that points to the URL where an XML file is stored. You are passing it the XML itself which of course is an invalid path. Try this instead:

XmlTextReader 构造函数接受一个字符串,该字符串指向存储 XML 文件的 URL。您将 XML 本身传递给它,这当然是无效的路径。试试这个:

using (var client = new WebClient())
{
    var xml = client.DownloadString("http://api.tr.im/api/trim_url.xml?url=" + HttpUtility.UrlEncode(txtURL.Text));
    using (var strReader = new StringReader(xml))
    using (var reader = XmlReader.Create(strReader))
    {

    }
}

回答by JAY

private void csv2_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        DataSet dsSchema = new DataSet();
        dsSchema.ReadXml(@"C:\Working\Teradata\ssis\Sample.xml");
        StringReader sreader = new StringReader(ToXml(dsSchema));
         ds.ReadXmlSchema(sreader);
         ds.ReadXml(@"C:\Working\Teradata\ssis\Sample.xml");
        ExportTableToCsvString(ds.Tables["session"], true, @"C:\Working\Teradata\ssis\op\session.csv");
        BuildDynamicTable(ds, @"C:\Working\Teradata\ssis\op\");

    }
    public string ToXml(DataSet ds)
    {
        using (var memoryStream = new MemoryStream())
        {
            using
                   (
                   TextWriter streamWriter = new StreamWriter(memoryStream))
            {
                var xmlSerializer = new XmlSerializer(typeof(DataSet));
                xmlSerializer.Serialize(streamWriter, ds);
                return Encoding.UTF8.GetString(memoryStream.ToArray());
            }
        }
    }

回答by Satish Babu

Try this. It's working.

尝试这个。它正在工作。

            XmlDocument xmlDocument = new XmlDocument();

            using (var strReader = new StringReader("xml string object here"))
            {
                using (XmlReader xmlReader = XmlReader.Create(strReader, settings))
                {
                    xmlDocument.Load(xmlReader);
                }
            }