从XmlReader的字符串中删除\“转义字符”

时间:2021-05-12 00:10:07

I am creating an XmlReader from a string. This string contains an XML feed in string format.

我正在从字符串创建XmlReader。这个字符串包含一个字符串格式的XML提要。

A snippet of that XML in the string is:

字符串中的XML片段是:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<menu>
    <menuitem name=\"Tools\">
    </menuitem>
</menu>

So when the line below is executed:

因此,当执行下面的行:

XmlReader myXmlReader = XmlReader.Create(myInputString);

This error is generated:

这个错误是生成:

Illegal characters in path.

The issue is because of escape characters (\") in the string. How can I get around this?

问题在于字符串中的转义字符(\”)。我怎样才能避开这个问题呢?

Thanks.

谢谢。

1 个解决方案

#1


11  

Every word of your question is wrong.

你问题的每句话都是错的。

You're seeing \ characters because the debugger displays strings as valid string literals.
The string itself is fine. If you print it to the console, or use the Text Visualizer, you'll see the string's raw value.

您看到的是\字符,因为调试器将字符串显示为有效的字符串文字。弦本身是好的。如果您将它打印到控制台,或者使用文本可视化工具,您将看到字符串的原始值。

XmlReader.Create() is throwing an exception because it takes a path to a file, not a string of source.
To parse a string of XML source, create a new StringReader from that string and pass that instead.

create()抛出一个异常,因为它接受的是文件的路径,而不是字符串。要解析一串XML源,可以从该字符串创建一个新的StringReader,并传递它。


Finally, XmlReader is an extremely annoying API.
You should use LINQ to XML instead; simply call XElement.Parse().

最后,XmlReader是一个非常烦人的API。您应该使用LINQ到XML;简单地调用XElement.Parse()。

#1


11  

Every word of your question is wrong.

你问题的每句话都是错的。

You're seeing \ characters because the debugger displays strings as valid string literals.
The string itself is fine. If you print it to the console, or use the Text Visualizer, you'll see the string's raw value.

您看到的是\字符,因为调试器将字符串显示为有效的字符串文字。弦本身是好的。如果您将它打印到控制台,或者使用文本可视化工具,您将看到字符串的原始值。

XmlReader.Create() is throwing an exception because it takes a path to a file, not a string of source.
To parse a string of XML source, create a new StringReader from that string and pass that instead.

create()抛出一个异常,因为它接受的是文件的路径,而不是字符串。要解析一串XML源,可以从该字符串创建一个新的StringReader,并传递它。


Finally, XmlReader is an extremely annoying API.
You should use LINQ to XML instead; simply call XElement.Parse().

最后,XmlReader是一个非常烦人的API。您应该使用LINQ到XML;简单地调用XElement.Parse()。