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

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

            #region delegates:

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

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

            #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属性)