繁体   English   中英

String.Replace不替换

[英]String.Replace not replacing

我正在xml文件中进行一些文件清理,并尝试使用string.Replace替换某些文本块,但它似乎并未替换我正在搜索的文本。

我的清理代码如下

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);
}

我应该能够写回原始的“数据”变量,但是我将变量拆分开来能够比较替换前后的字符串。 我的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>

并且前后字符串看起来相同。 我确定我缺少一些愚蠢的东西,但是我看不到它是什么。 对类似问题的大多数回答都指出,原始代码没有使用返回值,但是在这种情况下,我肯定使用的是返回值。

按照建议,我将发布最终解决此问题的代码。

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;
}

此代码有效,并且比原始代码不那么脆弱。 一如既往,欢迎提出建议。 感谢所有评论并引导我朝着这个答案的人,我对此表示赞赏。

暂无
暂无

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

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