Linq to XML 比较字符串
我正在尝试编写一段接收字符串的代码,使用该字符串中的数据对另一个字符串进行更改,然后保存另一个字符串
我更喜欢使用 linq 来执行此操作,因为我对它有点熟悉,尽管这并不是说我思想完全封闭。
无论如何,接收到的字符串的形式类似于
"<?xml version=\"1.0\" encoding=\"utf-8\"?><Root><Value><Code>AAA</Code><Description>First description</Description><Bool>Y</Bool></Value><Value><Code>BBB</Code><Description>Second description</Description><Bool>Y</Bool></Value><Value><Code>CCC</Code><Description>Third description</Description><Bool>N</Bool></Value></Root>";
或 具有正确的格式
"<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Root>
<Value>
<Code>AAA</Code>
<Description>First description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>BBB</Code>
<Description>Second description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>CCC</Code>
<Description>Third description</Description>
<Bool>N</Bool>
</Value>
</Root>"
,例如。另一个值与
"<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Root>
<Value>
<Code>111</Code>
<Description>111 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>AAA</Code>
<Description>First description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>222</Code>
<Description>222 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>BBB</Code>
<Description>Second description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>333</Code>
<Description>333 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>CCC</Code>
<Description>Third description</Description>
<Bool>Y</Bool>
</Value>
</Root>"
此类似,形式相同,但具有更多值,并且所有布尔值都设置为 Y。我想要做的就是找到布尔值设置为 N 的所有代码,并将新 XML 上的这些布尔值设置为 N。
因此,将这两者组合起来的结果将是新的 xml,但代码 CCC 的值会将 Bool 设置为 N。所以:
"<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Root>
<Value>
<Code>111</Code>
<Description>111 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>AAA</Code>
<Description>First description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>222</Code>
<Description>222 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>BBB</Code>
<Description>Second description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>333</Code>
<Description>333 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>CCC</Code>
<Description>Third description</Description>
<Bool>N</Bool>
</Value>
</Root>"
对我来说,似乎应该有一种非常简单的方法可以使用 Linq to XML 来做到这一点,但是我一直在努力一段时间以来,我似乎对 XML 缺乏经验,因为我在这方面遇到了很多麻烦。
任何帮助将不胜感激。
谢谢
I'm trying to write a piece of code which receives a string, uses the data in this string to make changes to another string then saves the other string
I would prefer to do this using linq as I'm somewhat familiar with it, although that's not to say that I'm completely close minded.
Anyway, the string being received is in a form like
"<?xml version=\"1.0\" encoding=\"utf-8\"?><Root><Value><Code>AAA</Code><Description>First description</Description><Bool>Y</Bool></Value><Value><Code>BBB</Code><Description>Second description</Description><Bool>Y</Bool></Value><Value><Code>CCC</Code><Description>Third description</Description><Bool>N</Bool></Value></Root>";
or with proper formatting
"<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Root>
<Value>
<Code>AAA</Code>
<Description>First description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>BBB</Code>
<Description>Second description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>CCC</Code>
<Description>Third description</Description>
<Bool>N</Bool>
</Value>
</Root>"
and, for example. the other value is like
"<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Root>
<Value>
<Code>111</Code>
<Description>111 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>AAA</Code>
<Description>First description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>222</Code>
<Description>222 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>BBB</Code>
<Description>Second description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>333</Code>
<Description>333 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>CCC</Code>
<Description>Third description</Description>
<Bool>Y</Bool>
</Value>
</Root>"
so of the same form but with more values and with all Bools set to Y. All I want to do is to find all the codes with a bool set to N and set those Bools on the new XML to N.
so the result of combining both of these would be the new xml but the Value with code CCC would have the Bool set as N. so:
"<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Root>
<Value>
<Code>111</Code>
<Description>111 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>AAA</Code>
<Description>First description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>222</Code>
<Description>222 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>BBB</Code>
<Description>Second description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>333</Code>
<Description>333 description</Description>
<Bool>Y</Bool>
</Value>
<Value>
<Code>CCC</Code>
<Description>Third description</Description>
<Bool>N</Bool>
</Value>
</Root>"
To me it seems as though there should be an incredibly easy way to do this using Linq to XML but I've been working at it for a while now and my inexperience with XML seems to be showing as I'm having quite a bit of trouble with this.
Any help would be much appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
类似的事情?
Something like that?
这是另一个解决方案,但它更像是使用 linq 而不是使用 linq:
Here is another solution but its more like using linq and not doing it with linq:
如果您的第一个 xml 存储在“doc1.xml”中,第二个 xml 存储在“doc2.xml”中,则只需执行以下操作即可实现您的目标:
上面的代码,基于以下操作操作
doc1
doc2
。然后,您可以将结果保存在新文件中,例如“doc3.xml”:If your first xml is stored in "doc1.xml" and the second one is stored in "doc2.xml", you can achieve your goal simply by doing this:
The above code, manipulates
doc1
based ondoc2
. You can then save the result in a new file like "doc3.xml":