views:

47

answers:

1

Hi, I have an xml file as following:

<?xml version="1.0" encoding="utf-8"?>
<ABC version="1" xmlns="urn:Company">
</ABC>

I am releasing version 2 and the namespace changed to "NewCompany". How do you update the namespace?

I tried

XmlDocument xmlDocument = new XmlDocument(); using (XmlReader xmlReader = XmlReader.Create("myfile.xml")) { xmlDocument.Load(xmlReader); }

        XmlNodeList nodeList = xmlDocument.GetElementsByTagName("ABC");
        if (nodeList.Count == 1)
        {
            XmlElement element = nodeList.Item(0) as XmlElement;
            if (element != null)
            {
                element.SetAttribute("xmlns", "NewCompany");

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                using (XmlWriter writer = XmlWriter.Create("myfile.xml", settings))
                {
                    xmlDocument.WriteTo(writer);
                }
            }
        }

But I get "The prefix '' cannot be redefined from to within the same start element tag." exception

A: 

You can try something similar to this post:

Amit