简体   繁体   English

如何使用C#(最好是LINQ)将XML文档中的所有“值”替换为“ 0.0”?

[英]How to replace all “values” in an XML document with “0.0” using C# (preferably LINQ)?

This is not a homework; 这不是家庭作业; I need this for my unit tests. 我的单元测试需要这个。

Sample input: <rows><row><a>1234</a><b>Hello</b>...</row><row>...</rows> . 输入示例: <rows><row><a>1234</a><b>Hello</b>...</row><row>...</rows>

Sample output: <rows><row><a>0.0</a><b>0.0</b>...</row><row>...</rows> . 示例输出: <rows><row><a>0.0</a><b>0.0</b>...</row><row>...</rows>

You may assume that the document starts with <rows> and that parent node has children named <row> . 您可以假定文档以<rows>开头,并且父节点具有名为<row> You do not know the name of nodes a , b , etc. 您不知道节点ab等的名称。

For extra credit: how to make this work with an arbitrary well-formed, "free-form" XML? 值得一提的是:如何使用任意格式正确的“自由格式” XML来实现此目的?

I have tried this with a regex :) without luck. 我尝试过一个正则表达式:)没有运气。 I could make it "non-greedy on the right", but not on the left. 我可以使它“在右边不贪心”,但不能在左边。 Thanks for your help. 谢谢你的帮助。

EDIT: Here is what I tried: 编辑:这是我尝试过的:

    private static string ReplaceValuesWithZeroes(string gridXml)
    {
        Assert.IsTrue(gridXml.StartsWith("<row>"), "Xml representation must start with '<row>'.");
        Assert.IsTrue(gridXml.EndsWith("</row>"), "Xml representation must end with '<row>'.");

        gridXml = "<deleteme>" + gridXml.Trim() + "</deleteme>"; // Fake parent.
        var xmlDoc = XDocument.Parse(gridXml);
        var descendants = xmlDoc.Root.Descendants("row");
        int rowCount = descendants.Count();
        for (int rowNumber = 0; rowNumber < rowCount; rowNumber++)
        {
            var row = descendants.ElementAt(0);
            Assert.AreEqual<string>(row.Value /* Does not work */, String.Empty, "There should be nothing between <row> and </row>!");
            Assert.AreEqual<string>(row.Name.ToString(), "row");

            var rowChildren = row.Descendants();
            foreach (var child in rowChildren)
            {
                child.Value = "0.0"; // Does not work.
            }
        }

        // Not the most efficient but still fast enough.
        return xmlDoc.ToString().Replace("<deleteme>", String.Empty).Replace("</deleteme>", String.Empty);
    }
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        foreach (XmlElement el in doc.SelectNodes("//*[not(*)]"))
            el.InnerText = "0.0";
        xml = doc.OuterXml;

or to be more selective about non-empty text nodes: 或对非空文本节点更具选择性:

        foreach (XmlText el in doc.SelectNodes("//text()[.!='']"))
            el.InnerText = "0.0";
XDocument xml = XDocument.Load(myXmlFile);

foreach (var element in xml.Descendants("row").SelectMany(r => r.Elements()))
{
    element.Value = "0.0";
}

Note that this general search for "Desscendants('row')" is not very efficient--but it satisfies the 'arbitrary format' requirement. 请注意,这种对“ Desscendants('row')”的一般搜索不是很有效-但它满足“任意格式”的要求。

You should take look at HTML Agility Pack . 您应该看看HTML Agility Pack It allows you to treat html documents as well-formed xml's, therefore you can parse it and change values. 它允许您将html文档视为格式正确的xml,因此您可以对其进行解析并更改值。

I think you can use Regex.Replace method in C#. 我认为您可以在C#中使用Regex.Replace方法。 I used the below regex to replace all the XML elements values: 我使用以下正则表达式替换了所有XML元素值:

[>]+[a-zA-Z0-9]+[<]+

This will basically match text starting with a '>'{some text alphabets or number}'<' . 这基本上将匹配以'>'{some text alphabets or number}'<'

I was able to use this successfully in Notepad++. 我能够在Notepad ++中成功使用它。 You can write a small program as well using this. 您也可以使用此程序编写一个小程序。

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

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