在 C# 中从 XML 添加/删除元素
我有这个 XML:
<states>
<state name="Alaska" colour="#6D7B8D">
<Location Name="loc1">
<Address>a1</Address>
<DateNTime>a2</DateNTime>
</Location>
<Location Name="loc2">
<Address>b1</Address>
<DateNTime>b2</DateNTime>
</Location>
</state>
<state name="Wyoming" colour="#6D7B8D">
<Location Name="loc3">
<Address>c1</Address>
<DateNTime>c2</DateNTime>
</Location>
<Location Name="loc4">
<Address>d1</Address>
<DateNTime>d2</DateNTime>
</Location>
</state>
</states>
我需要从州添加/删除位置。我该怎么做呢?谁能用一个例子解释如何使用 Linq?
I have this XML:
<states>
<state name="Alaska" colour="#6D7B8D">
<Location Name="loc1">
<Address>a1</Address>
<DateNTime>a2</DateNTime>
</Location>
<Location Name="loc2">
<Address>b1</Address>
<DateNTime>b2</DateNTime>
</Location>
</state>
<state name="Wyoming" colour="#6D7B8D">
<Location Name="loc3">
<Address>c1</Address>
<DateNTime>c2</DateNTime>
</Location>
<Location Name="loc4">
<Address>d1</Address>
<DateNTime>d2</DateNTime>
</Location>
</state>
</states>
I need to Add/Delete locations from state. How can I go about doing this? Can anyone explain using Linq with an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Linq 不处理插入或删除。
但您可以使用 XLinq 库来实现这两者。
Linq doesn't handle inserts or deletes.
But you can use the XLinq library to both.
要添加节点,请搜索要添加的父元素,创建要添加的元素,然后添加它。
要删除节点,请搜索要删除的节点,然后将其删除。
To add nodes, search for the parent element you wish to add to, create the element you want to add then add it.
To remove nodes, search for the nodes you wish to remove then remove them.
以下是如何执行您所要求的操作的示例:
Here is an example of how you can do what you are asking: