删除所有没有属性的 XML 元素 C#
我想知道如何从 xml 文件中删除没有 name 或 ref 类型第一个属性的所有元素。即使父元素已被删除,包含所需类型的第一个属性的子元素也必须保留,并且它们应该在层次结构中向上移动
例如,如果这是输入文件:
<xs:element name="Body" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType>
<xs:sequence>
<xs:element ref="Client" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Risk" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Claim" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Complaint" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="ClientFee" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="User" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
我期望以下输出:
<xs:element name="Body" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element ref="Client" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Risk" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Claim" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Complaint" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="ClientFee" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="User" minOccurs="0" />
</xs:element>
I would like to know how to remove all elements from an xml file which do not have a first attribute of type name or ref. The child elements that DO contain a first attribute of the required type must stay even if the parent has been deleted and they should just be moved up in the hierarchy
For example, if this is the input file:
<xs:element name="Body" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType>
<xs:sequence>
<xs:element ref="Client" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Risk" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Claim" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Complaint" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="ClientFee" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="User" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
I would expect the following output:
<xs:element name="Body" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element ref="Client" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Risk" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Claim" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="Complaint" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="ClientFee" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="User" minOccurs="0" />
</xs:element>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
例如这样:
For example this way: