开发者

How to replace all "values" in an XML document with "0.0" using C# (preferably LINQ)?

开发者 https://www.devze.com 2023-02-15 02:04 出处:网络
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>.

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>.

Sample output: <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>. You do not know the name of nodes a, b, etc.

For extra credit: how to make this work with an arbitrary well-formed, "free-form" 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.D开发者_运维问答escendants("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.


You should take look at HTML Agility Pack. It allows you to treat html documents as well-formed xml's, therefore you can parse it and change values.


I think you can use Regex.Replace method in C#. I used the below regex to replace all the XML elements values:

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

This will basically match text starting with a '>'{some text alphabets or number}'<'.

I was able to use this successfully in Notepad++. You can write a small program as well using this.

0

精彩评论

暂无评论...
验证码 换一张
取 消