【问题标题】:XElement is automatically adding xmlns="" to itselfXElement 会自动将 xmlns="" 添加到自身
【发布时间】:2011-08-22 18:56:26
【问题描述】:

我正在从一个表中创建一个新的 XDocument。我必须从 XSD 文档中验证文档,并且它一直失败,因为它在不应该的时候将 xmlns="" 添加到元素之一。以下是相关的部分代码。

    XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
                XNamespace xmlns = "https://uidataexchange.org/schemas";
                XElement EmployerTPASeparationResponse = null;
                XElement EmployerTPASeparationResponseCollection = new XElement(xmlns + "EmployerTPASeparationResponseCollection", new XAttribute(XNamespace.Xmlns + "xsi", xsi), new XAttribute(xsi + "schemaLocation", "https://uidataexchange.org/schemas SeparationResponse.xsd"));
                XDocument doc = new XDocument(
                new XDeclaration("1.0", null, "yes"), EmployerTPASeparationResponseCollection);
    //sample XElement populate Element from database
    StateRequestRecordGUID = new XElement("StateRequestRecordGUID");
                        StateRequestRecordGUID.SetValue(rdr["StateRequestRecordGUID"].ToString());

    //sample to add Elements to EmployerTPASeparationResponse
    EmployerTPASeparationResponse = new XElement("EmployerTPASeparationResponse");
                    if (StateRequestRecordGUID != null)
                    {
                        EmployerTPASeparationResponse.Add(StateRequestRecordGUID);
                    }

    //the part where I add the EmployerTPASeparationResponse collection to the parent
    EmployerTPASeparationResponseCollection.Add(EmployerTPASeparationResponse);

以上代码生成以下xml文件。

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<EmployerTPASeparationResponseCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://uidataexchange.org/schemas SeparationResponse.xsd" xmlns="https://uidataexchange.org/schemas">
<EmployerTPASeparationResponse xmlns="">
    <StateRequestRecordGUID>94321098761987654321323456109883</StateRequestRecordGUID>
  </EmployerTPASeparationResponse>
</EmployerTPASeparationResponseCollection>

注意元素 EmployerTPASeparationResponse。它有一个空的 xmlns 属性。我想要发生的是只写没有任何属性的 EmployerTPASeparationResponse。

【问题讨论】:

    标签: c# linq-to-xml xml-namespaces


    【解决方案1】:

    当您创建所有其他元素(EmployerTPASeparationResponse 和 StateRequestRecordGUID)时,您应该在名称元素中包含命名空间(与创建 EmployerTPASeparationResponseCollection 时所做的相同。

    【讨论】:

    • 重点是我根本不希望在 EmployerTPASeparationResponse 中使用 xmlns 属性。有什么想法吗?
    • 它存在但带有空白值的事实意味着它根本不在命名空间中(或者更确切地说是空白的)。如果您删除命名空间属性(按照我所说的,并按照下面的达斯汀),那么您会发现该属性将不存在。虽然这意味着这些节点(及其子节点,除非明确覆盖)位于父节点的命名空间中。
    【解决方案2】:

    您需要指定要添加的元素的命名空间。例如

    //sample XElement populate Element from database
    StateRequestRecordGUID = new XElement(xmlns + "StateRequestRecordGUID");
    

    //sample to add Elements to EmployerTPASeparationResponse
    EmployerTPASeparationResponse = new XElement(xmlns + "EmployerTPASeparationResponse");
    

    【讨论】:

      【解决方案3】:

      您需要在添加XElement 时为其指定命名空间,使其与XDocument 的命名空间相匹配。你可以这样做:

      XElement employerTPASeperationResponse =
           new XElement(xmlns + "EmployerTPASeparationResponse");
      

      【讨论】:

      • 关键是我根本不想在 EmployerTPASeparationResponse 中使用 xmlns 属性。有什么想法吗?
      • @Zach:添加上面应该删除它,因为它位于已经指定它的元素下。架构会为您处理这部分。
      • 成功了!现在我必须在我添加元素的每个地方都包含 xmlns + "ElementName"。谢谢!!
      【解决方案4】:

      你要为根元素创建一个XNamespace,然后在创建元素时,把创建的对象命名空间,像这样:

      xmlDoc = new XDocument();
      xmlDoc.Declaration = new XDeclaration("1.0", "utf-8", null);
      
      XNamespace pageDefinition = @"http://xmlns.oracle.com/adfm/uimodel";
      
      XElement root = new XElement(pageDefinition + "pageDefinition", new XAttribute("Package", "oracle.webcenter.portalapp.pages"));
      
      xmlDoc.Add(root);
      

      以上代码生成如下xml文件:

      <?xml version="1.0" encoding="UTF-8"?>
      <pageDefinition xmlns="http://xmlns.oracle.com/adfm/uimodel" Package="oracle.webcenter.portalapp.pages"/>
      

      【讨论】:

        猜你喜欢
        • 2011-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-21
        • 1970-01-01
        • 2012-09-26
        相关资源
        最近更新 更多