本文介绍了如何为这个XML创建xelement?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这是我的请求xml。我需要在C#中转换为XElement。 < soapenv:envelope xmlns:soapenv = http://schemas.xmlsoap.org/soap/envelope/ xmlns:web = http ://webservices.bit.com xmlns:ter = http://tp.requests.objects.webservice.cc.bit.com > < soapenv:header > < web:authenticationtoken > < web:key > rgg3 < web:密码 > testenv < web:user > [email protected] < soapenv:body > < web:getsbyzip > < web:getsbyzipcoderequest > < ter:arraygetbyzipcode > <! - 零次或多次重复: - > < ter:getbyzipcode > < ter:servicelevel > KK < ter:servicewing > Takal < ter:getbyzipcode > < ter:servicelevel > KK < ter:servicewing > Takal 我的尝试: public XElement GetRequestXml(GetModel getModel) { XNamespace web = http://webservices.bit.com; XNamespace soapenv = http://schemas.xmlsoap.org/soap/envelope/; XNamespace ter = http://tp.requests.objects.webservice.cc.bit.com ; XElement requestXml = new XElement(soapenv + 信封, 新 XElement(soapenv + 标题, new XElement(web + AuthenticationToken, new XAttribute(web + key, rgg3 ), new XAttribute(web + password, testenv), 新的 XAttribute(web + user, [email protected]))), new XElement(soapenv + Body, 新 XElement(web + GetByZioCode, new XElement(web + GetByZipCodeRequest, new XElement(ter + arrayTerminalByZipCode , new XElement(ter + TerminalByZipCode , new XAttribute(ter + serviceLevel ,getModel.serviceLevel), new XAttribute(ter + servicewing,getModel.servicewing) new XElement(ter + TerminalByZipCode, new XAttribute(ter + serviceLevel,getModel.serviceLevel), new XAttribute (ter + servicewing,getModel.servicewing), )) )); return requestXml; } 我试过上面的代码。如果有任何帮助。在此先感谢。解决方案 您应该使用正确的对象: SoapFormatter Class(System.Runtime.Serialization.Formatters.Soap)| Microsoft Docs [ ^ ] 详情请见:C# SOAP序列化«John Nelson的博客 [ ^ ] c# - 如何在.NET SoapFormatter中控制命名空间? - 堆栈溢出 [ ^ ] This is my request xml. I need to convert to XElement in C#.<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservices.bit.com" xmlns:ter="http://tp.requests.objects.webservice.cc.bit.com"> <soapenv:header> <web:authenticationtoken> <web:key>rgg3 <web:password>testenv <web:user>[email protected] <soapenv:body> <web:getsbyzip> <web:getsbyzipcoderequest> <ter:arraygetbyzipcode> <!--Zero or more repetitions:--> <ter:getbyzipcode> <ter:servicelevel>KK <ter:servicewing>Takal <ter:getbyzipcode> <ter:servicelevel>KK <ter:servicewing>TakalWhat I have tried:public XElement GetRequestXml(GetModel getModel) { XNamespace web = "http://webservices.bit.com"; XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/"; XNamespace ter = "http://tp.requests.objects.webservice.cc.bit.com"; XElement requestXml = new XElement(soapenv + "Envelope", new XElement(soapenv + "Header", new XElement(web + "AuthenticationToken", new XAttribute(web + "key", "rgg3"), new XAttribute(web + "password", "testenv"), new XAttribute(web + "user", "[email protected]"))), new XElement(soapenv + "Body", new XElement(web + "GetByZioCode", new XElement(web + "GetByZipCodeRequest", new XElement(ter + "arrayTerminalByZipCode", new XElement(ter + "TerminalByZipCode", new XAttribute(ter + "serviceLevel", getModel.serviceLevel), new XAttribute(ter + "servicewing", getModel.servicewing) new XElement(ter + "TerminalByZipCode", new XAttribute(ter + "serviceLevel", getModel.serviceLevel), new XAttribute(ter + "servicewing", getModel.servicewing), )))); return requestXml; }I tried above code. if any help. Thanks in Advance. 解决方案 You should use proper object: SoapFormatter Class (System.Runtime.Serialization.Formatters.Soap) | Microsoft Docs[^]For further details, please see: C# SOAP Serialization « John Nelson's Blog[^]c# - How to control namespace in .NET SoapFormatter? - Stack Overflow[^] 这篇关于如何为这个XML创建xelement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 05:34