XElement会自动将xmlns=""添加到自己身上

22

我正在从一个表创建一个新的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而没有任何属性。

4个回答

12

你需要指定你要添加的元素的命名空间,例如:

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

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

10

当你添加XElement时,需要指定它的命名空间以匹配XDocument的命名空间。可以按照以下方式实现:

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

3
关键是我不想在EmployerTPASeparationResponse中包含xmlns属性。有什么想法吗? - Zach
3
@Zach:添加上述内容应该会将它删除,因为它位于已经指定了它的元素下面。体系结构会为您处理这部分。 - Jeff Yates
3
好的!现在我需要在每个添加元素的地方包含xmlns +“ElementName”。谢谢! - Zach

3

您需要为根元素创建一个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"/>

0

当您创建所有其他元素(EmployerTPASeparationResponse和StateRequestRecordGUID)时,应确保在名称元素中包含命名空间(就像在创建EmployerTPASeparationResponseCollection时所做的那样)。


重点是我不想在EmployerTPASeparationResponse中有xmlns属性。有什么想法吗? - Zach
它存在但为空值意味着它根本不在命名空间中(或者说是空的命名空间)。如果您删除命名空间属性(按照我所说的方法,以及Dustin下面的指示),则会发现该属性不存在。尽管暗示着这些节点(及其子节点,除非明确覆盖)位于父命名空间中。 - Reddog

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接