【发布时间】: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