具有不同本地名称的XDocument重复名称空间 [英] XDocument duplicate namespace with different Local Name

查看:73
本文介绍了具有不同本地名称的XDocument重复名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的XML文档:

I have an XML document that looks like this:

    <Schema Namespace="BBSF_Model" Alias="Self"
      p1:UseStrongSpatialTypes="false"
      xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
      xmlns:p1="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
      xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
    <EntityType Name="Customer">
        <Property Name="Id" Type="Guid" Nullable="false" />
    </EntityType>
  </Schema>

我使用以下代码修改了文档的属性元素:

I used the below code to modify the Property Element of the document:

    XElement csdlEentity = csdlDoc.Root.Descendants()
            .Where(d => d.Name.LocalName == "EntityType")
            .FirstOrDefault(e => e.Attribute("Name").Value == "Customer");

    var csdlProperty = csdlEentity.Descendants()
            .Where(d => d.Name.LocalName == "Property")
            .FirstOrDefault(e => e.Attribute("Name").Value == "Id");

    XNamespace annotation = "http://schemas.microsoft.com/ado/2009/02/edm/annotation";
    var attrib = new XAttribute(annotation + "StoreGeneratedPattern", "Computed");
    csdlProperty.Add(attrib);

当我保存XDocument时,属性元素如下所示:

When I save the XDocument it the Property Element looks like:

    <Property Name="Id" Type="Guid" Nullable="false" p1:StoreGeneratedPattern="Computed" />

但是我想要的是:

  <Property Name="Id" Type="Guid" Nullable="false" annotation:StoreGeneratedPattern="Computed" />

问题在于xmlns" http://schemas.microsoft.com /ado/2009/02/edm/annotation "在文档的根节点中被两次引用,具有两个不同的别名/LocalName(注释,p1)

The Problem is that the xmlns "http://schemas.microsoft.com/ado/2009/02/edm/annotation" is referenced twice in the root node of the document with two different aliases/LocalNames (annotation, p1)

    xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
    xmlns:p1="http://schemas.microsoft.com/ado/2009/02/edm/annotation"

我无法更改或篡改根节点.

I cannot change or tamper the root node.

如何保存文档或更新属性元素以提供所需的输出?

How can I save the document or update the Property Element to give the required output?

推荐答案

由于您的想法,我找到了一个解决方案. 总体思路是,我将p1的值更改为添加了新属性的任何内容,然后甚至在保存XDocument之前将p1返回其原始值.

I found a solution all thanks to your ideas. The general Idea is I changed the value of p1 to anything added my new attribute then returned p1 to its original value even before saving the XDocument.

XAttribute p1 = csdlDoc.Root.Attributes()
            .First(a => a.IsNamespaceDeclaration && a.Name.LocalName == "p1");

p1.Value = "http://schemas.microsoft.com/ado/2009/02/edm/annotation/TEMP";

......
// the same update now works as there is only one xmlns 
XNamespace annotation = "http://schemas.microsoft.com/ado/2009/02/edm/annotation";
var attrib = new XAttribute(annotation + "StoreGeneratedPattern", "Computed");

csdlProperty.Add(attrib);

p1.Value = "http://schemas.microsoft.com/ado/2009/02/edm/annotation/";

另一个可行的解决方案是交换根节点上的订单或声明(在注释之前声明p1),如下所示:

Another solution that worked is swapping the order or the declarations at the root node (declare p1 before annotation) like below:

<Schema Namespace="BBSF_Model" Alias="Self"
  p1:UseStrongSpatialTypes="false"
  xmlns:p1="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
  xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" 
  xmlns="http://schemas.microsoft.com/ado/2009/11/edm">

总的来说,它们看起来都是廉价的解决方案.

In general both look like cheap solutions..

这篇关于具有不同本地名称的XDocument重复名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆