且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除XML中的节点

更新时间:2022-10-22 21:30:00

没有这样的东西,并不是真的需要它。 .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