简体   繁体   English

C#用单引号替换双引号

[英]C# replace double quotes with single quote

I have a string that has two sets of double double quotes as below: 我有一个带有两组双引号的字符串,如下所示:

string ns = @"<tns:Event xmlns:tns=""http://sbr.gov.au/comn/event.02.data"">";

I need to replace the two sets of double double quotes around the namespace with a single quote but I'm struggling to see how to do it. 我需要用单引号替换命名空间周围的两组双双引号,但我正在努力查看如何做到这一点。 I'm sure this is pretty easy but I'm pretty new to C#. 我敢肯定这很容易,但是我对C#还是很陌生。

Thanks in advance. 提前致谢。

Just to expand on my wider issue a little. 只是为了扩大我的广泛问题。 Below is the code I'm using within an SSIS script component: 以下是我在SSIS脚本组件中使用的代码:

string xml = Row.RejectReason.ToString();
XDocument xDoc = XDocument.Parse(xml);
XNamespace tns = "http://sbr.gov.au/comn/event.02.data";
var eventItems = xDoc.Element(tns + "Event").Element(tns + "EventItems").Elements(tns + "EventItem");
foreach (var eventItem in eventItems)
{
   Row.RejectReason = eventItem.Element(tns + "Short.Description").Value;
}

The Parsing of the XML is failing due to the the double double quotes around the namespace within the XML. XML的解析失败,原因是XML内的名称空间周围有双双引号。 That XML fragment is coming from a CSV file. 该XML片段来自CSV文件。

when use @ before a string, you must type "" as ". At the end, ns contins 当在字符串前使用@时,必须键入“”作为“。”最后,ns继续

<tns:Event xmlns:tns="http://sbr.gov.au/comn/event.02.data">

or you can do this: 或者您可以这样做:

string ns = "<tns:Event xmlns:tns=\"http://sbr.gov.au/comn/event.02.data\">";

but no different 但没有什么不同

You can use string's Replace() method: 您可以使用字符串的Replace()方法:

ns = ns.Replace('"', '\'');

or 要么

ns = ns.Replace("\"", "'");

You can try a regex. 您可以尝试使用正则表达式。 Something like 就像是

var checkDoubleQuotes = @"""";
ns = Regex.Replace(ns, checkDoubleQuotes, "\"");

You can use an escape character for the quote. 您可以使用转义字符作为报价。 See this mdn article: Link 请参阅此mdn文章: 链接

You can replace "" with \\' for single quote 您可以用单引号将“”替换为“”

 Regex.Replace(ns, @"""", "\'");

or replace "" with \\" for double quote 或用\\代替双引号

Regex.Replace(ns, @"""", "\"");

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

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