简体   繁体   English

xmlns =“”被不必要地添加

[英]xmlns=“” is unwantedly being added

Following is my XML request, I am a beginner. 以下是我的XML请求,我是初学者。

<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="user@mydomain.com" 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>

I am trying to create c# request for this but i am getting xmlns="" in Hotelavail tag which i do not want. 我正在尝试为此创建C#请求,但是我在Hotelavail标记中得到了xmlns =“”,这是我不想要的。

//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);

I have tried giving "SOAPENV_NS" in Hotelavail tag as I read it somewhere so xmlns="" wouldn't be added but no use I get the url as well. 我尝试在Hotelavail标记中提供“ SOAPENV_NS”,因为我在某处读取它,因此不会添加xmlns =“”,但也无济于事。

Any help would be much appreciated. 任何帮助将非常感激。

You're implicitly creating HotelAvail and all its children with an empty namespace (as you don't specify one in your calls to CreateElement ). 您正在使用空名称空间隐式创建HotelAvail及其所有子级(因为您在对CreateElement的调用中未指定一个)。

The 'default' namespace is defined in the root of your XML by xmlns="http://www.juniper.es/webservice/2007/" . “默认”名称空间是通过xmlns="http://www.juniper.es/webservice/2007/"在XML的根目录中定义的。

Change your CreateElement calls for HotelAvail and children to: 将您对HotelAvail和子级的CreateElement调用更改为:

XMLDoc1.CreateElement("HotelAvail", WKSP_NS);

See this fiddle for a working demo. 请参阅此小提琴以获得有效的演示。

As an aside, I'd agree with Jon Skeet that you should probably investigate LINQ to XML rather than using the old XmlDocument API. 顺便说一句,我同意Jon Skeet的观点,您应该研究LINQ to XML,而不是使用旧的XmlDocument API。 It's much nicer to work with. 与它一起工作要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM