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

时间:2018-03-21 11:48:06   收藏:0   阅读:999
       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();

  

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!