Modifying namespace in XML document programmatically

static XElement stripNS(XElement root) {
return new XElement(
root.Name.LocalName,
root.HasElements ?
root.Elements().Select(el => stripNS(el)) :
(object)root.Value
);
}
static void Main() {
var xml = XElement.Parse(@"<?xml version=""1.0"" encoding=""utf-16""?>
<ArrayOfInserts xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<insert>
<offer xmlns=""http://schema.peters.com/doc_353/1/Types"">0174587</offer>
<type2 xmlns=""http://schema.peters.com/doc_353/1/Types"">014717</type2>
<supplier xmlns=""http://schema.peters.com/doc_353/1/Types"">019172</supplier>
<id_frame xmlns=""http://schema.peters.com/doc_353/1/Types"" />
<type3 xmlns=""http://schema.peters.com/doc_353/1/Types"">
<type2 />
<main>false</main>
</type3>
<status xmlns=""http://schema.peters.com/doc_353/1/Types"">Some state</status>
</insert>
</ArrayOfInserts>");
Console.WriteLine(stripNS(xml));
}

  

I needed to validate an XML document with a given XSD document. Seems easy enough… so let’s have a look at the schema first:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://my.namespace"
elementFormDefault="qualified"
targetNamespace="http://my.namespace">
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
<xs:element name="age" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

The XML instance is:

<?xml version="1.0" encoding="utf-8" ?>
<customer>
<firstname>Homer</firstname>
<lastname></lastname>
<age>36</age>
</customer>

The code is straightforward:

static void Main(string[] args)
{
// Load the xml document
XDocument source = XDocument.Load(@"instance.xml");
// Load the schema
XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
xmlSchemaSet.Add(null, XmlReader.Create(@"customer.xsd"));
// Validate
try { source.Validate(xmlSchemaSet, ValidationCallback, true); }
catch (Exception ex) { Console.WriteLine(ex.Message); }
}
static void ValidationCallback(object sender,
System.Xml.Schema.ValidationEventArgs e)
{
Console.WriteLine(string.Format("[{0}] {1}", e.Severity, e.Message));
}

If you run this, no errors are thrown so it seems to validate. To be sure, let’s change the age in an invalid value:

<Age>invalid!</Age>

and test again. Well… actually, no validation error is thrown in this case either… what’s going on here?

Actually, the XML is not validated at all, because it’s not in the same namespace (http://my.namespace) as the schema definition. This is very dangerous, as we might easily get mislead by thinking that it validates because no errors are thrown. So how do we solve it?

We could ask the sender to provide the correct namespace in the XML file – this would be the best solution because then it would just work – if you try to validate the following XML:

<?xml version="1.0" encoding="utf-8" ?>
<customer xmlns="http://my.namespace">
<firstname>Homer</firstname>
<lastname></lastname>
<age>invalid</age>
</customer>

…then the validation error is thrown, because the namespaces now match:

Unfortunately, it is not always possible to change the XML file, so how can we bypass this namespace conflict? If appears that if we would change the namespace in the loaded XML document to the one we are using in our schema, the conflict is resolved. A first attempt may be:

// Load the xml document
XDocument source = XDocument.Load(@"instance.xml");
// Change namespace to reflect schema namespace
source.Root.SetAttributeValue("xmlns", "http://my.namespace");
// Load the schema
XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
xmlSchemaSet.Add(null, XmlReader.Create(@"customer.xsd"));
// Validate
try { source.Validate(xmlSchemaSet, ValidationCallback, true); }
catch (Exception ex) { Console.WriteLine(ex.Message); }

If we run this, the validation error is still not thrown, so setting the namespace attribute is not enough. The reason is that once the XDocument is loaded, every element in the tree gets prefixed with the namespace name. So we need to change them all, and so I wrote the following method that does this:

static void Main(string[] args)
{
// Load the xml document
XDocument source = XDocument.Load(@"instance.xml");
// Change namespace to reflect schema namespace
source = SetNamespace(source,"http://my.namespace");
// Load the schema
XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
xmlSchemaSet.Add(null, XmlReader.Create(@"customer.xsd"));
// Validate
try { source.Validate(xmlSchemaSet, ValidationCallback, true); }
catch (Exception ex) { Console.WriteLine(ex.Message); }
}
public static XDocument SetNamespace(XDocument source, XNamespace xNamespace)
{
foreach (XElement xElement in source.Descendants())
{
// First make sure that the xmlns-attribute is changed
xElement.SetAttributeValue("xmlns", xNamespace.NamespaceName);
// Then also prefix the name of the element with the namespace
xElement.Name = xNamespace + xElement.Name.LocalName;
}
return source;
}
static void ValidationCallback(object sender,
System.Xml.Schema.ValidationEventArgs e)
{
Console.WriteLine(string.Format("[{0}] {1}", e.Severity, e.Message));
}

The SetNameSpace method will set the corrrect namespace for each element in the XDocument. And if we run it now, the validation error is thrown again because the namespace in the XDocument has been modified and matches the schema namespace.

 
 
 
 

3 thoughts on “Modifying namespace in XML document programmatically”

  1. Janez says:

    Thanks, a working solution to a problem that took the better part of my day. :-)

  2. Jim says:

    This solution was very hard to fine…thanks so much for posting it.

  3. Mike says:

    This was very helpful and got me past some serious frustration! I was changing a child element tree to match a parent namespace, but I did not want to have the extra size of including the SetAttributeValue on all elements. My change was a change from one default namespace to another existing and prefixed one. This did the trick for me. Below are some minor adjustments that might be useful to others in some cases.

    public static XDocument SetNamespace(XDocument source, XNamespace original, XNamespace target)
    {
    //First change the element name (and namespace)
    foreach (XElement xElement in source.Descendants().Where(x => x.Name.Namespace == original))
    xElement.Name = target + xElement.Name.LocalName;

    //Second, remove the default namespace attribute.
    foreach (XElement xElement in source.Descendants().Where(x => x.Attributes().Where(y => y.Name == “xmlns”).Count() > 0))
    xElement.Attribute(“xmlns”).Remove();

    return source;
    }

Leave a Reply

Modifying namespace in XML document programmatically的更多相关文章

  1. parsing XML document from class path resource

    遇到问题:parsing XML document from class path resource [spring/resources] 解决方法:项目properties— source—remo ...

  2. eclipse错误:Unable to read workbench state. Workbench UI layout will be reset.XML document structures

    Unable to read workbench state. Workbench UI layout will be reset.XML document structures must start ...

  3. parsing XML document from class path resource [config/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [config/applicationContext.xml] 解决方案

    parsing XML document from class path resource [config/applicationContext.xml]; nested exception is j ...

  4. 解决SoapFault (looks like we got no XML document)问题

    今天在调试项目的时候出现下面的错误信息: SoapFault looks like we got no XML document (D:\phpStudy\WWW\self.shop.xunmall. ...

  5. 出错: IOException parsing XML document from ServletContext resource [/cn.mgy.conig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/cn.mgy.conig]

    错误的详细内容: 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanDefinitionStoreExceptio ...

  6. (转)EVMON_FORMAT_UE_TO_TABLES procedure - move an XML document to relational tables

    原文:https://www.ibm.com/support/knowledgecenter/zh/SSEPGG_9.8.0/com.ibm.db2.luw.sql.rtn.doc/doc/r0054 ...

  7. org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: c

    //这个是 配置文件放错了地方 org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing ...

  8. IOException parsing XML document from class path resource [WebRoot/WEB-INF/applicationContext.xml];

    parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io. ...

  9. Office 365 - For security reasons DTD is prohibited in this XML document

    博客地址:http://blog.csdn.net/FoxDave 今天在测试东西的时候发现在本机运行CSOM代码或使用Office 365 PowerShell时,出现了如下错误: Connec ...

随机推荐

  1. Webview窗口设置遮罩层

    在Webview窗口中如果存在子Webview的情况下,使用html中的css来做页面遮罩无法覆盖子Webview,为了解决此问题,WebviewStyle对象添加mask属性,用于设置Webview ...

  2. HTTP1.1协议-RFC2616-中文版

    转自:http://www.cnblogs.com/k1988/archive/2010/01/12/2165683.html 说明 本文档规定了互联网社区的标准组协议,并需要讨论和建议以便更加完善. ...

  3. vue使用路由跳转到上一页

    this.$router.go(-1) <template> <div> <button class="btn btn-success" @click ...

  4. ota升级动画背景色修改

    https://wenku.baidu.com/view/0d63ad25192e45361066f549.html https://blog.csdn.net/huangyabin001/artic ...

  5. VIP之Switch

    Switch II 最大能连接12路输入与12路输出 不能合并数据数 每个输入可以驱动多个输出 每个输出只能被一个输入驱动 当输入没有连接到输出时,可以禁止掉 每个被禁止的输入可以设置成停止或者消耗模 ...

  6. 实战C++对象模型之成员函数调用

    先说结论:C++的类成员函数和C函数实质是一样的,只是C++类成员函数多了隐藏参数this. 通过本文的演示,可以看见这背后的一切,完全可C函数方式调用C++类普通成员函数和C++类虚拟成员函数. 为 ...

  7. sublime使用技巧汇总

    sublime使用技巧 Ubuntu下安装sublime text 3143版本 Install the GPG key: wget -qO - https://download.sublimetex ...

  8. 第三周Access的总结

    一.问;这节课你学到了什么知识? 答:这周我学得比较少,主要是学Access的数据库进行基本的维护. 2.3数据库的基本维护 对Access定期检查,修复是整个数据库重要部分: 1.Access可修复 ...

  9. git-创建新项目

    1.一般第一次使用git,需要进行全局设置,如果下次创建新项目或者fork别人的项目,则不需要再进行设置:但是如果想要提交到不同的代码管理网站,则需要再设置,比如现在我的是在gitlab.com上进行 ...

  10. zookeeper学习day01

    1.zkAPI:(借助闭锁来实现)    1)创建闭锁对象  2)创建zk对象  3)连接zk客户端(连接成功执行countDown方法)  4)执行await方法(保证链接成功) 5)zk对象调用对 ...