如何删除XML中的节点 [英] How to delete node in XML

查看:81
本文介绍了如何删除XML中的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除此xml文件中的每个course_code?!



 <   course     xmlns:xsi   =  http://www.w3.org/2001/XMLSchema-instance    xmlns:xsd   =  http://www.w3.org/2001/XMLSchema >  
< course_code > 30 < ; / course_code >
< totalnum > 20 < / totalnum >
< 名称 > asd < / name >
< coursecat > 工程< / coursecat >
< / course >
< 课程 xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance xmlns:xsd = http://www.w3.org/2001/XMLSchema >
< course_code < span class =code-keyword>> 20 < / course_code >
< totalnum < span class =code-keyword>> 30 < / totalnum >
< name < span class =code-keyword>> hh < / name >
< coursecat > 艺术< / coursecat >
< / course >
< 课程 xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance xmlns:xsd = http:// www .w3.org / 2001 / XMLSchema >
< <温泉n class =code-leadattribute> course_code > 12 < / course_code >
< < span class =code-leadattribute> totalnum > 20 < / totalnum >
< < span class =code-leadattribute> name > dd < / name >
< < span class =code-leadattribute> coursecat > 艺术< / coursecat >
< / course >
< 讲师 xmlns:xsi = http://www.w3.org/2001/ XMLSchema-instance xmlns:xsd = http://www.w3.org/2001/XMLSchema >
< instructor_id > 30 < / instructor_id >
< name > omar < / name >
< phone_num > 010 < / phone_num >
< 地址 > alex < / address >
< 电子邮件 > moas @ yahoo < / email >
< course_id2 > 12 < / course_id2 >
< / instructor >





我尝试过:



XML移到问题部分并添加了代码块 - OriginalGriff] [/ edit]

解决方案

没有这样的东西,并不是真的需要它。 .NET FCL提供的所有与XML相关的类型都遵循不同的原则:您必须立即编写整个XML。例如,您可以删除XML DOM模型中的节点,但仍需编写完整的XML。如果您考虑一下,您会明白这是一个可行的决定。



这是我对可用XML类的简短概述:



  1. 使用 System.Xml.XmlDocument 类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
  2. 使用类 System.Xml .XmlTextWriter System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx
  3. 使用类 System.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx http://msdn.microsoft.com/en-us/library/bb387063.aspx


  4. -SA


how to delete every course_code in this xml file ?!

<course xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <course_code>30</course_code>
  <totalnum>20</totalnum>
  <name>asd</name>
  <coursecat>Engineering</coursecat>
</course>
<course xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <course_code>20</course_code>
  <totalnum>30</totalnum>
  <name>hh</name>
  <coursecat>Art</coursecat>
</course>
<course xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <course_code>12</course_code>
  <totalnum>20</totalnum>
  <name>dd</name>
  <coursecat>Art</coursecat>
</course>
<instructor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <instructor_id>30</instructor_id>
  <name>omar</name>
  <phone_num>010</phone_num>
  <address>alex</address>
  <email>moas@yahoo</email>
  <course_id2>12</course_id2>
</instructor>



What I have tried:

[edit]XML moved to question section and code block added - OriginalGriff][/edit]

解决方案

There is no such thing, and it's not really needed. All the XML-related types offered by .NET FCL work on different principles: you have to write the whole XML at once. You can, say, delete a node in the XML DOM Model, but you still have to write whole XML. If you think about it, you will understand that this is a viable decision.

This is my short overview of available XML classes:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx.
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx, http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx.
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx, http://msdn.microsoft.com/en-us/library/bb387063.aspx.


—SA


这篇关于如何删除XML中的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆