1. 程式人生 > >C# XML 去xmlns:xsd和xmlns:xsi屬性

C# XML 去xmlns:xsd和xmlns:xsi屬性

getch pac pos leg pub ati als nodes AC

       public static XElement WithoutNamespaces(this XElement element)
        {
            if (element == null) return null;

            #region delegates:

            Func<XNode, XNode> getChildNode = e => (e.NodeType == XmlNodeType.Element) ? (e as XElement).WithoutNamespaces() : e;

            Func<XElement, IEnumerable<XAttribute>> getAttributes = e => (e.HasAttributes) ?
                e.Attributes()
                    .Where(a => !a.IsNamespaceDeclaration)
                    .Select(a => new XAttribute(a.Name.LocalName, a.Value))
                :
                Enumerable.Empty<XAttribute>();

            #endregion

            return new XElement(element.Name.LocalName,
                element.Nodes().Select(getChildNode),
                getAttributes(element));
        }

  調用

 var xDoc = XDocument.Parse(xml);
 var xRoot = xDoc.Root.WithoutNamespaces();

  

C# XML 去xmlns:xsd和xmlns:xsi屬性