是的XElement自动添加的xmlns =""本身 [英] XElement is automatically adding xmlns="" to itself
问题描述
我创建从表中一个新的XDocument。我必须从XSD文件验证文档和它不断失败,因为它的xmlns =添加到要素之一,当它不应该。下面是是相关的部分代码。
I am creating a new XDocument from a table. I have to validate the document from an XSD document and it keeps failing because it add the xmlns="" to one of the Elements when it shouldn't. Here's parts of the code that are pertinent.
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文件。
The above code produces the following xml file.
<?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没有属性的。
Notice the element EmployerTPASeparationResponse. It has an empty xmlns attribute. What I want to happen is to just write EmployerTPASeparationResponse with no attributes at all.
推荐答案
您需要指定元素的命名空间您要添加。 。例如
You need to specify the namespace of the elements you are adding. e.g.
//sample XElement populate Element from database
StateRequestRecordGUID = new XElement(xmlns + "StateRequestRecordGUID");
和
//sample to add Elements to EmployerTPASeparationResponse
EmployerTPASeparationResponse = new XElement(xmlns + "EmployerTPASeparationResponse");
这篇关于是的XElement自动添加的xmlns =""本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!