移除XML文档里所有的命名空间
2010-04-08 12:35
274 查看
老外的方法移除XML文档里所有的命名空间
原谅请看:http://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c
{
XElement xe = XElement.Load(Server.MapPath("test.xml")); // 加载XML文档
XElement myxe = RemoveAllNamespaces(xe); // 调用下面中的一个方法
myxe.Save(Server.MapPath("test2.xml")); // 保存文档
Response.Write(myxe); // 这三行为输出预览,测试使用
Response.ContentType = "text/xml";
Response.End();
}
//Implemented based on interface, not part of algorithm 输出String 类型
public static string RemoveAllNamespaces(string xmlDocument)
{
XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument));
return xmlDocumentWithoutNs.ToString();
}
//Core recursion function 输出 XElement 类型
private static XElement RemoveAllNamespaces(XElement xmlDocument)
{
if (!xmlDocument.HasElements)
{
XElement xElement = new XElement(xmlDocument.Name.LocalName);
xElement.Value = xmlDocument.Value;
return xElement;
}
return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el)));
}
原谅请看:http://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c
{
XElement xe = XElement.Load(Server.MapPath("test.xml")); // 加载XML文档
XElement myxe = RemoveAllNamespaces(xe); // 调用下面中的一个方法
myxe.Save(Server.MapPath("test2.xml")); // 保存文档
Response.Write(myxe); // 这三行为输出预览,测试使用
Response.ContentType = "text/xml";
Response.End();
}
//Implemented based on interface, not part of algorithm 输出String 类型
public static string RemoveAllNamespaces(string xmlDocument)
{
XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument));
return xmlDocumentWithoutNs.ToString();
}
//Core recursion function 输出 XElement 类型
private static XElement RemoveAllNamespaces(XElement xmlDocument)
{
if (!xmlDocument.HasElements)
{
XElement xElement = new XElement(xmlDocument.Name.LocalName);
xElement.Value = xmlDocument.Value;
return xElement;
}
return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el)));
}
相关文章推荐
- Android之xml文档中的命名空间
- 读取带命名空间的Xml文档的方法备忘
- Schema的命名空间支持---6.2.2: 命名空间对XML文档的作用
- JavaScript 实现命名空间(namespace)的最佳方案——兼容主流的定义类(class)的方法,兼容所有浏览器,支持用JSDuck生成文档
- 多命名空间的 Xml 文档解析
- [玩转微信平台]XML的格式化- 如何去掉XML 文档头和命名空间
- 当xml中存在命名空间,dom4j解析以及写入xml文档时的乱码问题
- XPath获取带命名空间的XML
- 循环对XML文档添加Attribute以及移除Element 【转】
- 资源文件xml里的命名空间
- XML的命名空间与python解析方法
- xml的schema命名空间之研究
- java学习脚印:SAX解析xml时命名空间以及参数问题(敏捷版)
- 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
- XML 读写(命名空间)
- 【原创】C#操作XML(带命名空间)
- .NET 对含有命名空间的xml解析
- dom4j 为生成 XML 的文件添加 xmlns(命名空间) 属性
- Android自定义XML属性以及遇到的命名空间的问题
- ASP.NET AJAX 说明文档->客户端引用->全局命名空间->JavaScript 基础类型扩展->Array 类型扩展->clear 函数