2016-12-14 30 views
0

以下是我的XML请求,我是一名初学者。xmlns =“”被无意中添加

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://www.juniper.es/webservice/2007/"> 
<soapenv:Header/> 
<soapenv:Body> 
<HotelAvail> 
<HotelAvailRQ Version="1.1" Language="en"> 
<Login Email="[email protected]" Password="pass" /> 
<Paxes> 
<Pax IdPax="1"> 
<Age>8</Age> 
</Pax> 
</Paxes> 
<HotelRequest> 
<SearchSegmentsHotels> 
<SearchSegmentHotels Start="2013-08-20" End="2013-08-22" DestinationZone="1953"/> 
<CountryOfResidence>ES</CountryOfResidence> 
<Boards> 
<Board Type="AD"/> 
</Boards> 
</SearchSegmentsHotels> 
<RelPaxesDist> 
<RelPaxDist> 
<RelPaxes> 
<RelPax IdPax="1"/> 
</RelPaxes> 
</RelPaxDist> 
</HotelRequest> 
<AdvancedOptions> 
<ShowHotelInfo>true</ShowHotelInfo> 
</AdvancedOptions> 
</HotelAvailRQ> 
</HotelAvail> 
</soapenv:Body> 
</soapenv:Envelope> 

我想创建c#请求这个,但我得到xmlns =“”在Hotelavail标记,我不想要。

//Declaration 
      const string SOAPENV_NS = "http://schemas.xmlsoap.org/soap/envelope/"; 
      const string WKSP_NS = "http://www.juniper.es/webservice/2007/"; 
      XmlDeclaration xmlDeclaration = XMLDoc1.CreateXmlDeclaration("1.0", "utf-16", null); 
      //root 
      XMLsoapenv = XMLDoc1.CreateElement("soapenv", "Envelope", SOAPENV_NS); 
      XMLsoapenv.SetAttribute("xmlns:soapenv", SOAPENV_NS); 
      XMLsoapenv.SetAttribute("xmlns", WKSP_NS); 
      //XMLDoc1.AppendChild(XMLsoapenv); 
      //header 
      XMLsoapenvHeader = XMLDoc1.CreateElement("soapenv", "Header", SOAPENV_NS); 
      XMLsoapenv.AppendChild(XMLsoapenvHeader); 
      XMLsoapenvBody = XMLDoc1.CreateElement("soapenv", "Body", SOAPENV_NS); 
      XMLsoapenv.AppendChild(XMLsoapenvBody); 
      //XMLDoc1.AppendChild(XMLsoapenv); 
      XMLHotelAvail = XMLDoc1.CreateElement("HotelAvail"); 
      XMLHotelAvailRQ = XMLDoc1.CreateElement("HotelAvailRQ"); 
      XMLHotelAvailRQ.SetAttribute("Version", "1.1"); 
      XMLHotelAvailRQ.SetAttribute("Language", language1); 
      XMLLogin = XMLDoc1.CreateElement("Login"); 
      XMLLogin.SetAttribute("Email", email); 
      XMLLogin.SetAttribute("Password", password); 
      XMLHotelAvailRQ.AppendChild(XMLLogin); 
      XMLPaxes = XMLDoc1.CreateElement("Paxes"); 
      XMLPaxFirstChild = XMLDoc1.CreateElement("Pax"); 
      XMLPaxFirstChild.SetAttribute("IdPax", "1"); 
      XMLPaxFirstChild.SetAttribute("Age", searchCriteria.FirstChild); 
      XMLPaxes.AppendChild(XMLPaxFirstChild); 
      XMLHotelAvailRQ.AppendChild(XMLPaxes); 
      XMLHotelRequest = XMLDoc1.CreateElement("HotelRequest"); 
      XMLSearchSegmentsHotels = XMLDoc1.CreateElement("SearchSegmentsHotels"); 
      XMLSearchSegmentHotels = XMLDoc1.CreateElement("SearchSegmentHotels"); 
      XMLSearchSegmentHotels.SetAttribute("Start", searchCriteria.CheckInDate); 
      XMLSearchSegmentHotels.SetAttribute("End", searchCriteria.CheckOutDate); 
      XMLSearchSegmentHotels.SetAttribute("DestinationZone", "628"); 
      XMLCountryOfResidence = XMLDoc1.CreateElement("CountryOfResidence", searchCriteria.Country); 
      XMLSearchSegmentHotels.AppendChild(XMLCountryOfResidence); 
      XMLSearchSegmentsHotels.AppendChild(XMLSearchSegmentHotels); 
      XMLHotelRequest.AppendChild(XMLSearchSegmentsHotels); 
      XMLRelPaxesDist = XMLDoc1.CreateElement("RelPaxesDist"); 
      XMLRelPaxDist = XMLDoc1.CreateElement("RelPaxDist"); 
      XMLRelPaxes = XMLDoc1.CreateElement("RelPaxes"); 
      XMLRelPax = XMLDoc1.CreateElement("RelPax"); 
      XMLRelPax.SetAttribute("IdPax", "1"); 
      XMLRelPaxes.AppendChild(XMLRelPax); 
      XMLRelPaxDist.AppendChild(XMLRelPaxes); 
      XMLRelPaxesDist.AppendChild(XMLRelPaxDist); 
      XMLHotelRequest.AppendChild(XMLRelPaxesDist); 
      XMLHotelAvailRQ.AppendChild(XMLHotelRequest); 
      XMLHotelAvail.AppendChild(XMLHotelAvailRQ); 
      XMLsoapenv.AppendChild(XMLHotelAvail); 
      //XMLsoapenvBody.AppendChild(XMLHotelAvail); 
      //XMLsoapenv.AppendChild(XMLsoapenvBody); 
      XMLDoc1.AppendChild(XMLsoapenv); 

我都试过,因为我在其他地方见过这么的xmlns =“”不会加入,但没有用,我得到的URL以及给予“SOAPENV_NS”在Hotelavail标签。

任何帮助将不胜感激。

+0

欢迎来到Stack Overflow。请把它降低到[mcve] - 我相信你不需要这么多的XML或代码来代表问题。然后请确保您的代码格式尽可能可读 - 在发布之前使用预览来查看您的文章的外观。 –

+0

(不清楚这与XSLT或xmlhttprequest有什么关系 - 如果可能的话,我强烈建议使用LINQ to XML而不是旧的'XmlDocument' API。) –

+1

只需将正确的名称空间传入'CreateElement'电话,你应该设置 –

回答

0

您隐式地创建了HotelAvail及其所有子级空名称空间(因为您在CreateElement的调用中没有指定一个子级)。

“默认”命名空间在您的XML的根目录中定义为xmlns="http://www.juniper.es/webservice/2007/"

更改您的CreateElement呼吁HotelAvail和儿童:

XMLDoc1.CreateElement("HotelAvail", WKSP_NS); 

this fiddle了工作演示。另外,我同意Jon Skeet的观点,你应该研究LINQ to XML,而不是使用旧的XmlDocument API。与它合作更好。