首页 新闻 会员 周边 捐助

删除XML 中的一行数据

0
悬赏园豆:10 [已解决问题] 解决于 2012-12-13 15:58

运行后生成的XML 文件格式如下

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Files version="0.1">
  <File Name="kk" extension=".dwg" Location="" Guid="16634fe4-9986-4285-a3af-5c6d1cc10fec" version="0.1">
    <SubFiles>
      <File Name="jj" extension=".dwf" Location="\kk" Guid="d249ba4f-eeaa-4a2a-a503-00e98d7c6ef7" version="0.1">
        <SubFiles>
          <File Name="jj" extension=".jpg" Location="\kk" Guid="a182ace6-261e-411e-8930-17f0a65086b9" version="0.1" />
        </SubFiles>
      </File>
    </SubFiles>
  </File>
</Files>
现在我要删除XML文件中 extension=.dwf的所有行该怎么实现。

 

下面是本人写的代码貌似出现了问题,请高人指点,能给出源码非常的感激。

 

 private void delete()
        {
            // 创建一个XmlDocument类的对象
            XmlDocument xmlDoc = new XmlDocument();

            // 把你想要读取的xml文档加载进来
            xmlDoc.Load("xmlfile.xml");

            // 读取你指定的节点
            XmlNodeList xnl = xmlDoc.SelectSingleNode("SubFiles").ChildNodes;

            foreach (XmlNode xn in xnl)
            {
                XmlElement xe = (XmlElement)xn;

                if (xe.GetAttribute("Extention")==".dwf")
                {
                    xe.RemoveAll();
                }
            }
            xmlDoc.Save("xmlfile.xml");

        
        }

问题补充: 写的更新的函数 public void Save(string xmlfile) { // Prevent the version is (0,0) that is considered as invalid file version for clients. if(this.ConfigVersion.Major == 0 && this.ConfigVersion.Minor == 0) { this.ConfigVersion = new Version(0, 1); } XmlTextWriter xmlWriter = new XmlTextWriter(xmlfile, Encoding.UTF8); xmlWriter.WriteStartDocument(true); xmlWriter.WriteStartElement("Files"); xmlWriter.WriteStartAttribute("version"); xmlWriter.WriteString(m_version.ToString()); xmlWriter.WriteEndAttribute(); foreach (IInvFileInfo file in this) { // The sub files should be skipped in this case and will be handled in the next for each. if (file.Extension.Equals(".idw") || file.Extension.Equals(".dwg")) { xmlWriter.WriteStartElement("File"); SaveInventorFileAttributesIntoXml(xmlWriter, file); if (file.RefFiles != null && file.RefFiles.Count > 0) { xmlWriter.WriteStartElement("SubFiles"); foreach (IInvFileInfo sub in file.RefFiles) { xmlWriter.WriteStartElement("File"); SaveInventorFileAttributesIntoXml(xmlWriter, sub); //symbol text info if (sub.SymbolTextInfo != null && sub.SymbolTextInfo.IsInstanced && sub.SymbolTextInfo.IsPromptedSymbol) { xmlWriter.WriteStartElement("SymbolTextInfo"); xmlWriter.
cici.fan的主页 cici.fan | 初学一级 | 园豆:46
提问于:2011-01-19 22:49
< >
分享
最佳答案
0

protected void Page_Load(object sender, EventArgs e)
{
string filepath = Server.MapPath(".") + "\\XMLFile.xml";

XmlDocument document
= new XmlDocument();
document.Load(filepath);

XmlNodeList list
= document.GetElementsByTagName("File");

for (int i = list.Count - 1; i >= 0; i--)
{
XmlNode node
= list[i];
if (node.Attributes["extension"].Value.Equals(".dwf"))
{
node.ParentNode.RemoveChild(node);
}
}

this.txt.Text = document.DocumentElement.OuterXml;
}

 

这样不知道行不行.~

收获园豆:4
Localhost | 菜鸟二级 |园豆:443 | 2011-01-20 09:27
你哪个txt 是什么
cici.fan | 园豆:46 (初学一级) | 2011-01-20 10:11
是页面上的一个textbox
Localhost | 园豆:443 (菜鸟二级) | 2011-01-20 11:17
其他回答(2)
0

有两处需改一下:

XmlNodeList xnl = xmlDoc.SelectSingleNode("//Files//File//SubFiles").ChildNodes;
//…………………………
if (xe.GetAttribute("extension") == ".dwf")

 

收获园豆:4
邀月 | 园豆:25475 (高人七级) | 2011-01-20 09:56
删不了呀
支持(0) 反对(0) cici.fan | 园豆:46 (初学一级) | 2011-01-20 10:10
@cici.fan: 是否没有操作文件权限?我已经删除了!
支持(0) 反对(0) 邀月 | 园豆:25475 (高人七级) | 2011-01-20 10:17
嗯,删除了,但是节点没有删。你能不能帮我一个忙
支持(0) 反对(0) cici.fan | 园豆:46 (初学一级) | 2011-01-21 16:37
0

使用XPATH来查找具有这样属性的节点

//查找属性extension='.dwg'的所有节点
XmlNodeList xnl = xmlDoc.SelectNodes("//*[@extension='.dwg']");

//同样:查找属性extension='.dwg'的file节点 //file[@extension='.dwg']

foreach (XmlNode xn in xnl)
{
try
{
xmlDoc.RemoveChild(xn);
//从XML中移除当前节点(包括子节点)
}
catch
{
//可能出现当前节点为前某个被移除节点的子节点
continue;
}
}

 

收获园豆:2
Shiaupo | 园豆:272 (菜鸟二级) | 2011-01-21 15:11
private void Load(string xmlfile)//获取XML 中的数据 { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlfile); XmlNode root = xmlDoc.SelectSingleNode("Files"); if (root != null) { XmlNode attri = root.Attributes.GetNamedItem("version"); if (attri != null) { m_version = new Version(attri.Value); } XmlNodeList fileList = root.SelectNodes("File"); foreach (XmlNode node in fileList) { IInvFileInfo file = CreateInventorFileByXmlNode(node); XmlNode subroot = node.SelectSingleNode("SubFiles"); if (subroot != null) { XmlNodeList subFileList = subroot.SelectNodes("File"); //sub files node List<IInvFileInfo> subFiles = new List<IInvFileInfo>(); foreach (XmlNode subNode in subFileList) //Inv File { //sub file node IInvFileInfo subFile = CreateInventorFileByXmlNode(subNode); XmlNode thumbnailNode = subNode.SelectSingleNode("SubFiles");//thumbnail if (thumbnailNode != null) { XmlNode thumbnailFileNode = thumbnailNode.SelectSingleNode("File"); if (thumbnailFileNode != null) { IInvFileInfo thnFile = CreateInventorFileByXmlNode(thumbnailFileNode); files.Add(thnFile.Guid, thnFile); subFile.RefFiles = new List<IInvFi
支持(0) 反对(0) cici.fan | 园豆:46 (初学一级) | 2011-01-22 14:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册