0

I want to remove xmlns part from the xml string and convert that to a json.

string test = "<Behavior xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.adlibsoftware.com\">\r\n  <JobFolders>\r\n    <Error>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Error>\r\n    <Work>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Work>\r\n    <Input>\r\n      <DeleteEmptySubfolders>true</DeleteEmptySubfolders>\r\n    </Input>\r\n  </JobFolders>\r\n  <JobFiles>\r\n    <ProcessingLocation>\r\n      <Server>\r\n        <TransferSegmentSize unit=\"Kilobytes\">4096</TransferSegmentSize>\r\n      </Server>\r\n    </ProcessingLocation>\r\n    <Input>\r\n      <Naming>Resh</Naming>\r\n    </Input>\r\n  </JobFiles>\r\n</Behavior>";

I tried using the below code but still not able to remove it. Any help would be great!

XmlDocument doc = new XmlDocument();

doc.LoadXml(test);

foreach (var node in doc)
{
    var el = node as XmlElement;
    
    if (el != null)
    {
        if (el.HasAttribute("xmlns"))
        {
            var ns = el.GetAttribute("xmlns");

            if (ns != null && ns == el.NamespaceURI)
            {
                el.RemoveAttribute("xmlns");
            }
        }
    }
}

string jsonText = JsonConvert.SerializeXmlNode(doc);

The output that I expect is:

{"Behavior":"JobFolders":{"Error":"${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}","Work":"${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}","Input":{"DeleteEmptySubfolders":"true"}},"JobFiles":{"ProcessingLocation":{"Server":{"TransferSegmentSize":{"@unit":"Kilobytes","#text":"4096"}}},"Input":{"Naming":"Resh"}}}}

The output I receive from above code:

{"Behavior":{"@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","JobFolders":{"Error":"${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}","Work":"${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}","Input":{"DeleteEmptySubfolders":"true"}},"JobFiles":{"ProcessingLocation":{"Server":{"TransferSegmentSize":{"@unit":"Kilobytes","#text":"4096"}}},"Input":{"Naming":"Resh"}}}}
Matthias Müller
  • 444
  • 6
  • 15
Jess
  • 41
  • 8
  • You can't convert this to json even after removing the xml namespace. Can you explain more in context of what you're trying to do and show expected outcome? – Trevor Dec 10 '21 at 16:17
  • Thank you for the update, but the output you've shown isn't valid json, in short deserialization wouldn't work. – Trevor Dec 10 '21 at 16:18
  • Instead of doing all of this, why not load an xmldocument and then serialize the document? For example using Newtonsoft: `string json = JsonConvert.SerializeXmlNode(doc);` – Trevor Dec 10 '21 at 16:21
  • @zaggler The string that I have has 2 xmlns attributes and by the above code I am able to remove only 1 attribute. Is there anyway that I could remove both the xmlns attribute? – Jess Dec 10 '21 at 16:22
  • string json = JsonConvert.SerializeXmlNode(doc); This doesnt remove the xmlns attribute right? But I want them to get removed. – Jess Dec 10 '21 at 16:24
  • No it will not remove them, that would only serialize the xml to json. You then could parse that json and or convert it to a defined type if you need. – Trevor Dec 10 '21 at 16:26
  • see: [How to remove all namespaces from XML with C#?](https://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c) – Luuk Dec 10 '21 at 16:26
  • @Jess you'll need to remove them and after you could serialize the rest to json... – Trevor Dec 10 '21 at 16:31
  • @zaggler yes that was my question exactly. And I have found a solution for it. Thank you! – Jess Dec 10 '21 at 16:39
  • @Jess [Please see](https://dotnetfiddle.net/u9qzQ0) that example... – Trevor Dec 10 '21 at 17:01
  • Does this answer your question? [How to remove all namespaces from XML with C#?](https://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c) – Metro Smurf Dec 10 '21 at 17:12

2 Answers2

2

Problem got solved. I have added the edited solution here. Thanks for your time!

  string test = "<Behavior xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.adlibsoftware.com\">\r\n  <JobFolders>\r\n    <Error>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Error>\r\n    <Work>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Work>\r\n    <Input>\r\n      <DeleteEmptySubfolders>true</DeleteEmptySubfolders>\r\n    </Input>\r\n  </JobFolders>\r\n  <JobFiles>\r\n    <ProcessingLocation>\r\n      <Server>\r\n        <TransferSegmentSize unit=\"Kilobytes\">4096</TransferSegmentSize>\r\n      </Server>\r\n    </ProcessingLocation>\r\n    <Input>\r\n      <Naming>Resh</Naming>\r\n    </Input>\r\n  </JobFiles>\r\n</Behavior>";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(test);

            foreach (var node in doc)
            {
                var el = node as XmlElement;
                if (el != null)
                {
                    if (el.HasAttribute("xmlns") || el.HasAttribute("xmlns:xsi"))
                    {
                        var ns = el.GetAttribute("xmlns");
                        var ns1 = el.GetAttribute("xmlns:xsi");
                        if (ns != null && ns == el.NamespaceURI && ns1 != null && ns1!= el.NamespaceURI)
                        {
                            el.RemoveAttribute("xmlns");
                            el.RemoveAttribute("xmlns:xsi");
                        }
                    }
                }
            }

            string jsonText = JsonConvert.SerializeXmlNode(doc);
Jess
  • 41
  • 8
0

The code in the question only removes 1 attribute per node.

This code is actually removing the attributes:

            string test = "<Behavior xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.adlibsoftware.com\">\r\n  <JobFolders>\r\n    <Error>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Error>\r\n    <Work>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Work>\r\n    <Input>\r\n      <DeleteEmptySubfolders>true</DeleteEmptySubfolders>\r\n    </Input>\r\n  </JobFolders>\r\n  <JobFiles>\r\n    <ProcessingLocation>\r\n      <Server>\r\n        <TransferSegmentSize unit=\"Kilobytes\">4096</TransferSegmentSize>\r\n      </Server>\r\n    </ProcessingLocation>\r\n    <Input>\r\n      <Naming>Resh</Naming>\r\n    </Input>\r\n  </JobFiles>\r\n</Behavior>";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(test);

            foreach (var node in doc)
            {
                var el = node as XmlElement;
                if (el != null)
                {
                    while (el.HasAttributes)
                    {
                        XmlAttribute item = el.Attributes[0];
                        if (el.HasAttribute(item.Name) && item.Name.Contains("xmlns"))
                        {
                            var ns = el.GetAttribute(item.Name);
                            if (ns != null )
                            {
                                el.RemoveAttribute(item.Name);
                                Console.WriteLine($"Remove attriute: {item.Name}"); 
                            }
                        }
                    }
                }
            }

            string jsonText = JsonConvert.SerializeXmlNode(doc);
            Console.WriteLine(jsonText);
            Console.ReadLine();
Luuk
  • 12,245
  • 5
  • 22
  • 33