如何替换所有“值”在带有“0.0”的 XML 文档中使用 C#(最好是 LINQ)?
这不是家庭作业;而是家庭作业。我需要这个来进行单元测试。
示例输入:
。
示例输出:
。
您可以假设文档以
开头,并且父节点具有名为
的子节点。您不知道节点 a
、b
等的名称。
额外加分:如何使用任意格式良好的“自由格式”XML 来实现此操作?
我已经用正则表达式尝试过这个:)但没有运气。我可以让它“在右边非贪婪”,但不能在左边。感谢您的帮助。
编辑:这是我尝试过的:
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);
}
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.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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者对非空文本节点更有选择性:
or to be more selective about non-empty text nodes:
请注意,这种对“后代('行')”的一般搜索效率不是很高,但它满足了“任意格式”的要求。
Note that this general search for "Desscendants('row')" is not very efficient--but it satisfies the 'arbitrary format' requirement.
您应该查看 HTML Agility Pack。它允许您将 html 文档视为格式良好的 xml 文档,因此您可以解析它并更改值。
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.
我认为你可以在 C# 中使用 Regex.Replace 方法。我使用下面的正则表达式来替换所有 XML 元素值:
[>]+[a-zA-Z0-9]+[<]+
这基本上会匹配以
'>'{一些文本字母或数字}'<'
。我能够在 Notepad++ 中成功使用它。您也可以使用它编写一个小程序。
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.