2

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?

4
  • What did you try? Where are you stuck? Commented Mar 16, 2016 at 13:11
  • Why are you working in terms of strings when the .NET Framework has an entire namespace dedicated to XML tooling that understands XML natively? Commented Mar 16, 2016 at 13:11
  • And, also, why are you trying to implement a method that suggests it makes changes when it's returning void and .NET strings are immutable? Commented Mar 16, 2016 at 13:12
  • Possible duplicate of how to change xml string value on fly Commented Mar 16, 2016 at 13:26

3 Answers 3

3

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)
            {
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Does Descendants find tag with name adminPassword?
Sure. Just change the tag name in the code above from password to adminPassword.
No, as I said in my question I would like to find all tags with password in name( I mean name.Contains("password"))
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.
Okay, I've updated the example accordingly to look for any instance of password, case-insensitive, in a full console app example.
|
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.