1

I need to remove link inside result tag by code.

Here is my xml file:

<?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>

But i want to empty result tag by code.I have used this code:

 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...
 }

I need to remove links inside result tag.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
deepika
  • 131
  • 1
  • 1
  • 3
  • Which of the links? There are three attributes on the result node that has links in their values. – mortb Aug 15 '13 at 12:04
  • @mortb i need to remove xsd files becuse they are creating problems.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 – deepika Aug 15 '13 at 12:05

2 Answers2

0

If you just want to get rid of everything on the <result> element (including the xmlns), then do this:

XmlDocument xmldoc = new XmlDocument();

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

    xmldoc.Load(xtr);
}

xmldoc.DocumentElement.RemoveAllAttributes();

That will produce this:

<?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>

If you want to get rid of specific ones then use RemoveAttribute(...).

Colin Smith
  • 12,375
  • 4
  • 39
  • 47
  • i need to remove data inside this :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 – deepika Aug 15 '13 at 12:08
  • can you please tell what i need to add in this code so i get value of OrderRef 52177.......right now i am able to get value of MailingGUID 3699f54b-a05c-45d9-9f91-2d65fea9e2f3 – deepika Aug 15 '13 at 12:21
0

Add at the top of your class file:

using System.Text.RegularExpressions;

and you may try this:

XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath("~/XMLFile1.xml"));
xml.DocumentElement.RemoveAllAttributes();
xml.LoadXml(Regex.Replace(xml.OuterXml, @"xmlns="".*?""", m => ""));
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78