删除 xml 文档中存在的属性

发布于 2024-11-19 13:17:07 字数 337 浏览 2 评论 0原文

如果文档中存在属性,如何从 XmlDocument 中删除该属性?请帮忙。我正在使用RemoveAttribute,但如何检查它是否存在。

root.RemoveAttribute(fieldName);

谢谢..

 <?xml version="1.0" standalone="yes" ?> 
 <Record1>
  <Attribute1 Name="DataFieldName" Value="Pages" /> 
 </Record1> 

我正在尝试删除名为“DataFieldName”的属性。

How to remove the attribute from XmlDocument if attribute exists in the document? Please help. I am using RemoveAttribute but how can I check if it exists.

root.RemoveAttribute(fieldName);

Thanks..

 <?xml version="1.0" standalone="yes" ?> 
 <Record1>
  <Attribute1 Name="DataFieldName" Value="Pages" /> 
 </Record1> 

I am trying to remove attribute named "DataFieldName".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋你朝朝暮暮 2024-11-26 13:17:07

不确定你到底想做什么,所以这里有两个例子。

删除属性:

var doc = new System.Xml.XmlDocument();
doc.Load("somefile.xml");
var root = doc.FirstChild;

foreach (System.Xml.XmlNode child in root.ChildNodes)
{
    if (child.Attributes["Name"] != null)
        child.Attributes.Remove(child.Attributes["Name"]);
}

将属性设置为空字符串:

var doc = new System.Xml.XmlDocument();
doc.Load("somefile.xml");
var root = doc.FirstChild;

foreach (System.Xml.XmlNode child in root.ChildNodes)
{
    if (child.Attributes["Name"] != null)
        child.Attributes["Name"].Value = "";
}

编辑:如果您详细说明原始请求,我可以尝试修改我的代码。一个 XML 文档只能有一个根节点,您的根节点似乎是 record1。那么这是否意味着您的整个文件将只包含一条记录?或者你的意思是有类似的东西

<?xml version="1.0" standalone="yes" ?>
<Records>
    <Record>
        <Attribute Name="DataFieldName" Value="Pages" />
    </Record>
    <Record>
        <Attribute Name="DataFieldName" Value="Pages" />
    </Record>
</Records>

Not sure exactly what you're trying to do, so here's two examples.

Removing the attribute:

var doc = new System.Xml.XmlDocument();
doc.Load("somefile.xml");
var root = doc.FirstChild;

foreach (System.Xml.XmlNode child in root.ChildNodes)
{
    if (child.Attributes["Name"] != null)
        child.Attributes.Remove(child.Attributes["Name"]);
}

Setting the attribute to an empty string:

var doc = new System.Xml.XmlDocument();
doc.Load("somefile.xml");
var root = doc.FirstChild;

foreach (System.Xml.XmlNode child in root.ChildNodes)
{
    if (child.Attributes["Name"] != null)
        child.Attributes["Name"].Value = "";
}

Edit: I can try to modify my code if you elaborate on your original request. An XML document can only have one root node and yours appears to be record1. So does that mean your entire file will only contain a single record? Or did you mean to have something like

<?xml version="1.0" standalone="yes" ?>
<Records>
    <Record>
        <Attribute Name="DataFieldName" Value="Pages" />
    </Record>
    <Record>
        <Attribute Name="DataFieldName" Value="Pages" />
    </Record>
</Records>
天暗了我发光 2024-11-26 13:17:07

您可以使用 XmlNamedNodeMap.RemoveNamedItem 方法(名称)来执行此操作。它可用于 Attributes。如果未找到匹配的节点,它将返回从此 XmlNamedNodeMap 中删除的 XmlNode 或空引用(在 Visual Basic 中为 Nothing)。

    [C#] 
    using System;
    using System.IO;
    using System.Xml;

   public class Sample
   {
    public static void Main()
    {
     XmlDocument doc = new XmlDocument();
     doc.LoadXml("<book genre='novel' publicationdate='1997'> " +
                 "  <title>Pride And Prejudice</title>" +
                 "</book>");      

     XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

     // Remove the publicationdate attribute.
     attrColl.RemoveNamedItem("publicationdate");

     Console.WriteLine("Display the modified XML...");
     Console.WriteLine(doc.OuterXml);

     }
   }

youcan use XmlNamedNodeMap.RemoveNamedItem Method (name) to do it. It can be used to Attributes.It will return the XmlNode removed from this XmlNamedNodeMap or a null reference (Nothing in Visual Basic) if a matching node was not found.

    [C#] 
    using System;
    using System.IO;
    using System.Xml;

   public class Sample
   {
    public static void Main()
    {
     XmlDocument doc = new XmlDocument();
     doc.LoadXml("<book genre='novel' publicationdate='1997'> " +
                 "  <title>Pride And Prejudice</title>" +
                 "</book>");      

     XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

     // Remove the publicationdate attribute.
     attrColl.RemoveNamedItem("publicationdate");

     Console.WriteLine("Display the modified XML...");
     Console.WriteLine(doc.OuterXml);

     }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文