修改XmlElement InnerText而不删除子节点

时间:2016-12-06 12:21:25

标签: c#

如果我有一个带有以下InnerXml的XmlElement:

ab/cd <text:line-break /> efg

如何使用以下InnerXml将其分解为三个XmlElements,即使任何斜杠(不是换行元素的一部分!)放入单独的元素中:

ab
/
cd <text:line-break /> efg

我目前有以下内容,但它会删除换行元素,因为它只适用于InnerText:

if (Span.InnerText.Contains("/"))
{
    // Create a new span for the slash
    var SlashSpan = Span.Clone();
    Span.ParentNode.InsertAfter(SlashSpan, Span);
    SlashSpan.InnerText = "/";

    // Create a copy of the original span for the text after the slash
    var SpanClone = Span.Clone();
    Span.ParentNode.InsertAfter(SpanClone, SlashSpan);
    SpanClone.InnerText = SpanClone.InnerText.Substring(SpanClone.InnerText.IndexOf("/") + 1);

    // Remove the slash and the text after it from the original span
    Span.InnerText = Span.InnerText.Substring(0, Span.InnerText.IndexOf("/"));
}

1 个答案:

答案 0 :(得分:0)

我不喜欢它,但它有效:

while (true)
{
    var SpanList = BodyNode.SelectNodes(".//text:span");
    var AnySlashesMoved = false;
    foreach (XmlElement Span in SpanList)
    {
        if (Span.InnerText.Contains("/") && Span.InnerXml != "/")
        {
            AnySlashesMoved = true;
            // Create a new span for the slash
            var SlashSpan = Span.Clone();
            Span.ParentNode.InsertAfter(SlashSpan, Span);
            SlashSpan.InnerText = "/";
            // Create a copy of the original span for the text after the slash
            var SpanClone = Span.Clone();
            Span.ParentNode.InsertAfter(SpanClone, SlashSpan);
            var FirstCNToKeep = -1;
            for (var I = 0; I < SpanClone.ChildNodes.Count; I++)
            {
                var CN = SpanClone.ChildNodes[I];
                if (CN.NodeType == XmlNodeType.Text && CN.Value.Contains("/"))
                {
                    CN.Value = CN.Value.Substring(CN.Value.IndexOf("/") + 1);
                    FirstCNToKeep = I;
                    break;
                }
            }
            if (FirstCNToKeep != -1)
            {
                for (var I = FirstCNToKeep - 1; I >= 0; I--)
                {
                    SpanClone.RemoveChild(SpanClone.ChildNodes[I]);
                }
            }
            // Remove the slash and the text after it from the original span
            var LastCNToKeep = -1;
            for (var I = 0; I < Span.ChildNodes.Count; I++)
            {
                var CN = Span.ChildNodes[I];
                if (CN.NodeType == XmlNodeType.Text && CN.Value.Contains("/"))
                {
                    CN.Value = CN.Value.Substring(0, CN.Value.IndexOf("/"));
                    LastCNToKeep = I;
                    break;
                }
            }
            if (LastCNToKeep != -1)
            {
                for (var I = Span.ChildNodes.Count - 1; I > LastCNToKeep; I--)
                {
                    Span.RemoveChild(Span.ChildNodes[I]);
                }
            }
        }
    }
    if (AnySlashesMoved == false)
    {
        break;
    }
}

如果有人想提出建议,仍然乐意接受更好的答案。