简体   繁体   English

如何从 Xml 字符串中删除 xmlns 部分?

[英]How to remove xmlns part from a Xml string?

I want to remove xmlns part from the xml string and convert that to a json.我想从 xml 字符串中删除 xmlns 部分并将其转换为 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:我期望的 output 是:

  {"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:我从上面的代码收到的 output:

 {"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"}}}}

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);

The code in the question only removes 1 attribute per node.问题中的代码仅删除每个节点的 1 个属性。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM