gpt4 book ai didi

c# - trim() 一个xml文档中所有xml元素和属性的值使用c#

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:17 25 4
gpt4 key购买 nike

我正在尝试找出像这样获取 xml 的最简单方法:

<Car>
<Description Model="Ford ">Blue </Description>
</Car>

进入这个:

<Car>
<Description Model="Ford">Blue</Description>
</Car>

最佳答案

使用 LINQ to XML,怎么样:

foreach (var element in doc.Descendants())
{
foreach (var attribute in element.Attributes())
{
attribute.Value = attribute.Value.Trim();
}
foreach (var textNode in element.Nodes().OfType<XText>())
{
textNode.Value = textNode.Value.Trim();
}
}

认为 这应该有效...我认为您不需要使用 ToList 来避免在迭代时打扰事情,因为您没有更改XML 文档的结构,只是文本。

关于c# - trim() 一个xml文档中所有xml元素和属性的值使用c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17708297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com