1

我需要通过代码删除结果标签内的链接。

这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.cfhdocmail.com/TestAPI2/Result.xsd https://www.cfhdocmail.com/TestAPI2/Result.xsd" xmlns="https://www.cfhdocmail.com/TestAPI2/Result.xsd">
  <data>
    <key>MailingGUID</key>
  <value>3699f54b-a05c-45d9-9f91-2d65fea9e2f3</value>
  </data><data>
    <key>OrderRef</key>
  <value>52177</value>
  </data>
</result>

但我想通过代码清空结果标签。我使用了这段代码:

 XmlDocument xml = new XmlDocument();      
 xml.Load(Server.MapPath("~/XMLFile1.xml"));
 // var xdoc = XDocument.Load(xmlFile);
 var configuration = xml.DocumentElement.SelectSingleNode("result");  
 if (configuration != null)
 {               
     // code...
 }

我需要删除结果标签内的链接。

4

2 回答 2

0

在类文件的顶部添加:

using System.Text.RegularExpressions;

你可以试试这个:

XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath("~/XMLFile1.xml"));
xml.DocumentElement.RemoveAllAttributes();
xml.LoadXml(Regex.Replace(xml.OuterXml, @"xmlns="".*?""", m => ""));
于 2013-08-15T13:09:08.540 回答
0

如果您只想删除元素上的所有<result>内容(包括 xmlns),请执行以下操作:

XmlDocument xmldoc = new XmlDocument();

using (XmlTextReader xtr = new XmlTextReader(Server.MapPath("~/XMLFile1.xml")))
{
    xtr.Namespaces = false;

    xmldoc.Load(xtr);
}

xmldoc.DocumentElement.RemoveAllAttributes();

这将产生:

<?xml version="1.0" encoding="utf-8"?>
<result>
  <data>
    <key>MailingGUID</key>
  <value>3699f54b-a05c-45d9-9f91-2d65fea9e2f3</value>
  </data><data>
    <key>OrderRef</key>
  <value>52177</value>
  </data>
</result>

如果您想摆脱特定的,请使用RemoveAttribute(...).

于 2013-08-15T12:04:27.510 回答