1

I have the following method

string UpdateXmlString(string xmlString) {...}

I would like to find all tags which name contain password and delete a value;

Before:

<job xmlns:i=\"...\" xmlns=\"...">
<password>asdfasdf</password>
<adminPassword>asd</adminPassword>
...</job>

Expected result:

<job xmlns:i=\"..." xmlns=\"...">
<password></password>
<adminPassword></adminPassword>
...</job>

How to implement this method?

3 Answers3

2

You should simply be using XmlDocument or XDocument to parse this. I wouldn't manipulate XML strings manually.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace XDocumentTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                String xml = "<?xml version=\"1.0\"?><rootElement>";
                xml += "<user id=\"1\"><password>temp</password></user>";
                xml += "<user id=\"2\"><adminPassword>foobar</adminPassword></user>";
                xml += "<user id=\"3\"><somePassWORDelement>foobarXYZ</somePassWORDelement></user>";
                xml += "</rootElement>";
                XDocument doc = XDocument.Parse(xml);
                foreach (XElement element in doc.Descendants().Where(
                    e => e.Name.ToString().ToLower().Contains("password")))
                {
                    Console.WriteLine(element);
                    // Delete your value here. Either changing the text node
                    // or by removing the password node. E.g.

                    element.Value = string.Empty;
                }

                Console.WriteLine(doc.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            while (Console.ReadKey(true).Key != ConsoleKey.Escape)
            {
            }
        }
    }
}
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
  • Does `Descendants` find tag with name `adminPassword`? –  Mar 16 '16 at 13:33
  • Sure. Just change the tag name in the code above from password to adminPassword. – ManoDestra Mar 16 '16 at 15:10
  • No, as I said in my question I would like to find all tags with `password` in name( I mean `name.Contains("password")`) –  Mar 16 '16 at 15:16
  • 1
    You could have been clearer on that point. Might be worth updating your question to state that the element name should CONTAIN password rather "with password in name". That seems to suggest that you want elements named password. I'll update my answer in a moment to reflect this new requirement. – ManoDestra Mar 16 '16 at 15:19
  • Okay, I've updated the example accordingly to look for any instance of password, case-insensitive, in a full console app example. – ManoDestra Mar 16 '16 at 15:34
  • foreach (XElement element in doc.Descendants().Where( e => e.Name.ToString().ToLower().Contains("password"))) { Console.WriteLine(element); // Delete your value here. Either changing the text node // or by removing the password node. E.g. element.Value = string.Empty; } Helped me to change values in an XML string I had. Thanks. – Shammie May 05 '23 at 13:42
0

You should use XPathNavigator.

In MSDN are some examples that will help you: https://msdn.microsoft.com/en-us/library/zx28tfx1(v=vs.110).aspx

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
0
var doc = XDocument.Load(path); 

var element = doc.Descendants("YOUR_Descendants")
                    .Where(arg => arg.Attribute("ExampleID").Value == "3" )// 
                    .Single();

element.Element("UpdateElements")
    .Element("UpdateElements_fc").Value = "222";// update
                doc.Save(path); //save
AOK
  • 118
  • 1
  • 1
  • 11