0

I am looking for a way, how to replace attribute value in XML node, where the issue with an attribute is, that its name is generated dynamically.

As an example, I can have two different XML files containing something like this

fileA.xaml

<root xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
    xmlns:p="clr-namespace:FullAssemblyIdentifier.Wrong_Namespace;assembly=AssemblyName">
</root>

fileB.xaml

<root xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
    xmlns:p2="clr-namespace:FullAssemblyIdentifier.Wrong_Namespace;assembly=AssemblyName">
</root>

As you can see, there is actually a difference in name. xmlns:p vs xmlns:p2. The thing is, that it can be called anyhow. It can even be xmlns:ABC. What I am trying to do is to create some kind of ReferenceChanger class, that will fix the reference in all XAML files at once. So far I was thinking about something like this:

ReferenceChanger.cs

public void ChangeReference(XmlNode xmlDoc)
{
    const string newReference = "FullAssemblyIdentifier.CorrectReference";

    // this does not select the element
    var element = (XmlElement)xmlDoc.SelectSingleNode("//root[contains(.,'FullAssemblyIdentifier.Wrong_Namespace')]");

    // How do i get the name of the attribute ???
    string dynamicallyGeneratedAttributeName = element.GetDynamicallyGeneratedAttributeIdentifier();

    if (element != null)
    {
        // this I can not use, because I do not know the attribute name
        element.SetAttribute(dynamicallyGeneratedAttributeName, newReference);
    }
}

One thing, that is certain is, that this wrong value is (and always will be) only in root element and not in any other element.

Kajiyama
  • 3,393
  • 8
  • 26
  • 38
  • Namespace prefix *should* make no difference... Are you sure you need what you trying to do? (some explanation what your actual goal may help) – Alexei Levenkov Mar 03 '18 at 06:55
  • I think it is kinda obvious from the example. What is the goal of this is, that we have some XAML generated using WPF application. While doing some refactoring, we decided to restructure our solution and move `classA` from `namespaceA` to `namespaceB`. Problem is, that all the old files, created in the past, do have reference to `namespaceA` which leads to incorrect XAML format and file does not open. – Kajiyama Mar 03 '18 at 08:23
  • I'm still not sure what you want to achieve... But it is very strange that you wrote code that generates XML (implying some decent understanding of the topic) but used such an unrelated "value of attribute" term for "namespace prefix" (https://stackoverflow.com/questions/1181888/what-does-xmlns-in-xml-mean). Hopefully using correct term will help you to either fined an answer or at least clarify the question (also I'd add sample of XML you have and what you want to convert it to). – Alexei Levenkov Mar 04 '18 at 18:15

0 Answers0