简体   繁体   English

String.Replace不替换

[英]String.Replace not replacing

I'm doing some file clean up in an xml file and trying to use string.Replace to replace certain text blocks but it does not seem to be replacing the text that I am searching on. 我正在xml文件中进行一些文件清理,并尝试使用string.Replace替换某些文本块,但它似乎并未替换我正在搜索的文本。

My clean up code is follows 我的清理代码如下

private Stream PrepareFile(string path)
{
    string data = File.ReadAllText(path);

    var newData = data.Replace("<a:FMax xmlns:b=\"http://www.w3.org/2001/XMLSchema\" i:type=\"b:string\"/>", "<a:FMax>0</a:FMax>")
        .Replace("<a:KVy xmlns:b=\"http://www.w3.org/2001/XMLSchema\" i:type=\"b:string\"/>", "<a:KVy>0</a:KVy>")
        .Replace("<a:Td xmlns:b=\"http://www.w3.org/2001/XMLSchema\" i:type=\"b:string\"/>", "<a:Td>0</a:Td>")
        .Replace("<a:VyLim xmlns:b=\"http://www.w3.org/2001/XMLSchema\" i:type=\"b:string\"/>", "<a:VyLim>0</a:VyLim>");

    var newData2 = newData.Replace("<a:VxTableSxI3_2I xmlns:b=\"http://www.w3.org/2001/XMLSchema\" i:type=\"b:string\"/>", "<a:VxTableSxI3_2I>0</a:VxTableSxI3_2I>");

    byte[] bytes = Encoding.ASCII.GetBytes(newData2);
    return new MemoryStream(bytes);
}

I should be able to write back to the original 'data' variable, but I split the variables out to be able to compare the strings before and after the replace. 我应该能够写回原始的“数据”变量,但是我将变量拆分开来能够比较替换前后的字符串。 My xml file contains the following values(copied verbatim) 我的xml文件包含以下值(逐字复制)

<a:LongitudinalTracker z:Id="i58">
    <Name xmlns="http://schemas.datacontract.org/2004/07/HmsSim.EntityModule.BaseTypes" i:nil="true"/>
    <a:FMax xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string"/>
    <a:K>2</a:K>
    <a:KVy xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string"/>
    <a:Td xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string"/>
    <a:VyLim xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string"/>
</a:LongitudinalTracker>

And the before and after strings look identical. 并且前后字符串看起来相同。 I'm sure I am missing something silly, but I can't see what it is. 我确定我缺少一些愚蠢的东西,但是我看不到它是什么。 Most of the answers to similar questions point out that the original code is not using the return value, but in this case I am definitely using the return value. 对类似问题的大多数回答都指出,原始代码没有使用返回值,但是在这种情况下,我肯定使用的是返回值。

As suggested I am posting the code that ended up solving this. 按照建议,我将发布最终解决此问题的代码。

private Stream PrepareFile(string path)
{
    string data = File.ReadAllText(path);

    var xml = XDocument.Parse(data);

    XNamespace ns = "http://schemas.datacontract.org/2004/07/HmsSim.EntityModule.Entities.SimulationEntities.Track";
    var longTracker = from item in xml.Descendants(ns + "LongitudinalTracker") select item;

    foreach (var xElement in longTracker.Elements())
    {
        XNamespace nsI = "http://www.w3.org/2001/XMLSchema-instance";
        if (xElement.Attribute(nsI + "type") != null)
        {
            xElement.Attribute(nsI + "type").Remove();
            XAttribute attribute = new XAttribute(nsI + "nil", "true");
            xElement.Add(attribute);
        }
    }

    var latTracker = from item in xml.Descendants(ns + "LateralTracker") select item;

    foreach (var xElement in latTracker.Elements())
    {
        XNamespace nsI = "http://www.w3.org/2001/XMLSchema-instance";
        if (xElement.Attribute(nsI + "type") != null)
        {
            xElement.Attribute(nsI + "type").Remove();
            XAttribute attribute = new XAttribute(nsI + "nil", "true");
            xElement.Add(attribute);
        }
    }

    Stream stream = new MemoryStream();
    xml.Save(stream);
    // Rewind the stream ready to read from it elsewhere
    stream.Position = 0;

    return stream;
}

This code works and is less brittle than the original code. 此代码有效,并且比原始代码不那么脆弱。 As always, suggestions are welcome. 一如既往,欢迎提出建议。 Thanks to everyone who commented and led me towards this answer, I appreciate it. 感谢所有评论并引导我朝着这个答案的人,我对此表示赞赏。

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

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