提问人:RKh 提问时间:2/10/2023 最后编辑:RKh 更新时间:2/15/2023 访问量:196
命名空间 '' 中的顶部 XML 元素“Data”引用不同的类型
The top XML element 'Data' from namespace '' references distinct types
问:
我必须将 Active Directory 数据发送到第三方应用程序。此第三方应用程序提供 .WSDL URL,我已将其作为服务引用添加到我的 .NET 应用程序中。在此之后,我必须调用一个函数“Import”,它看起来像这样:
Import(Security security, string Data, int FolderID)
这里的“数据”是需要传输的XML数据。我有一个像这样的XML:
var xEle = new XElement("Document",
from emp in lstADUsers
select new XElement("Record",
new XElement("UserName", emp.UserName),
new XAttribute("FirstName", "TEST_FNAME"),
new XAttribute("LastName", "Test_LNAME"),
new XAttribute("Email", "[email protected]")
));
我将 Import 方法称为:
Import(token, xEle, 1)
当此方法被击中时,我收到以下错误:
命名空间 '' 中的顶部 XML 元素“Data”引用不同的类型 System.String 和 System.Byte[]。使用 XML 属性指定另一个 元素或类型的 XML 名称或命名空间。
第三方应用程序需要 SOAP 数据。
额外细节
SOAP 信封如下所示:
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:sec='http://schemas.xmlsoap.org/ws/2003/06/secext' xmlns:wsu='http://schemas.xmlsoap.org/ws/2003/06/utility'
xmlns:urn='urn:TestApp'>
<soapenv:Header>
<sec:Security>
<sec:UsernameToken wsu:Id='TestApp'>
<sec:Username>TestUser</sec:Username>
<!--Optional:-->
<sec:Password>TestPassword</sec:Password>
</sec:UsernameToken>
</sec:Security>
</soapenv:Header>
<soapenv:Body>
<urn:ImportData>
<Table></Table>
<FolderId>0</FolderId>
<Data><![CDATA[<Document>
<Record></Record>
</Document>
</Data>
代码更改如下
/// <summary>
/// Summary description for ThirdPArtyApp
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class ThirdParty : System.Web.Services.WebService
{
public Header header;
[WebMethod]
[SoapHeader("header", Direction = SoapHeaderDirection.InOut)]
public void CreateXmlForImport()
{
ThirdParty_Serv thirdParty = new ThirdParty_Serv();
header.Username = "test123";
header.Password = "XYZ123";
const string FILENAME = @"D:\Users\Documents\test.xml";
XDocument doc = XDocument.Load(FILENAME);
XNamespace nsUrn = doc.Root.GetNamespaceOfPrefix("urn");
XElement importData = doc.Descendants(nsUrn + "ImportData").FirstOrDefault();
XElement xCdata = new XElement("Document",
new XElement("Record",
new XAttribute("UserName", "T12345"),
new XAttribute("FirstName", "TEST_RP"),
new XAttribute("LastName", "Test_LNAME"),
new XAttribute("Email", "[email protected]")
));
string cDataStr = xCdata.ToString();
XCData cdata = new XCData(cDataStr);
thirdParty.ImportData("/Do/Persons", 0, cDataStr, 3);
}
}
答:
1赞
jdweng
2/10/2023
#1
我使用该架构来获得正确的结构。在 VS 中,您可以通过转到菜单 Project:Add New Item : Xml File 来测试语法。而不是将 xml 传递到视图中。错误将像编译器错误一样显示在错误列表中。此外,如果您键入一个左尖括号,将显示可以在任何部分中添加的元素。
我使用了以下 xml
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:sec='http://schemas.xmlsoap.org/ws/2003/06/secext' xmlns:wsu='http://schemas.xmlsoap.org/ws/2003/06/utility'
xmlns:urn='urn:TestApp'>
<soapenv:Header>
<sec:Security>
<sec:UsernameToken wsu:Id='TestApp'>
<sec:Username>TestUser</sec:Username>
<!--Optional:-->
<sec:Password>TestPassword</sec:Password>
</sec:UsernameToken>
</sec:Security>
</soapenv:Header>
<soapenv:Body>
<urn:ImportData>
<Table></Table>
<FolderId>0</FolderId>
<Record></Record>
</urn:ImportData>
</soapenv:Body>
</soapenv:Envelope>
这是我的代码
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XNamespace nsUrn = doc.Root.GetNamespaceOfPrefix("urn");
XElement importData = doc.Descendants(nsUrn + "ImportData").FirstOrDefault();
XElement xCdata = new XElement("Record",
new XElement("UserName", "username"),
new XAttribute("FirstName", "TEST_FNAME"),
new XAttribute("LastName", "Test_LNAME"),
new XAttribute("Email", "[email protected]")
);
string cDataStr = xCdata.ToString();
XCData cdata = new XCData(cDataStr);
importData.Add(cdata);
}
这是最终的 Xml
<?xml version="1.0"?>
<soapenv:Envelope xmlns:urn="urn:TestApp" xmlns:wsu="http://schemas.xmlsoap.org/ws/2003/06/utility" xmlns:sec="http://schemas.xmlsoap.org/ws/2003/06/secext" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<sec:Security>
<sec:UsernameToken wsu:Id="TestApp">
<sec:Username>TestUser</sec:Username>
<!--Optional:-->
<sec:Password>TestPassword</sec:Password>
</sec:UsernameToken>
</sec:Security>
</soapenv:Header>
<soapenv:Body>
<urn:ImportData>
<Table/>
<FolderId>0</FolderId>
<Record/>
<![CDATA[<Record FirstName="TEST_FNAME" LastName="Test_LNAME" Email="[email protected]"><UserName>username</UserName></Record>]]>
</urn:ImportData>
</soapenv:Body>
</soapenv:Envelope>
评论
1赞
jdweng
2/10/2023
CData 可以位于 Record Tag 中,也可以位于 ImportData 中。检查数据所属的位置。
0赞
RKh
2/13/2023
在原文中,它位于文档标签之前:<Data><![CDATA[<文档> <记录>
0赞
RKh
2/13/2023
您使用的“test.xml”需要什么?它包含什么?
0赞
jdweng
2/13/2023
Text.xml 是上面的原始 xml。XML 中的顺序无关紧要。
1赞
jdweng
2/15/2023
尝试一个干净的构建。看起来您更改了 Net/Core 的目标版本。并非所有中间 OBJ 文件都已编译。有些包含对未安装的不同版本的 Net/Core 的引用。或者需要将库添加到模块顶部的 using 语句中并添加缺少的引用。
评论