【问题标题】:xmlns="" is unwantedly being addedxmlns="" 被意外添加
【发布时间】:2017-04-29 12:58:57
【问题描述】:

以下是我的 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>

我正在尝试为此创建 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);

我尝试在 Hotelavail 标记中给出“SOAPENV_NS”,因为我在某处读到它,因此不会添加 xmlns="",但我也得到了 url。

任何帮助将不胜感激。

【问题讨论】:

  • 欢迎来到 Stack Overflow。请将其减少为minimal reproducible example - 我确定您不需要 this 太多 XML 或代码来表示问题。然后请确保您的代码格式尽可能可读 - 在发布之前使用预览查看您的帖子的外观。
  • (不清楚这与 XSLT 或 xmlhttprequest 有什么关系——如果可能的话,我强烈建议使用 LINQ to XML 而不是旧的XmlDocument API。)
  • 只需将正确的命名空间传递给CreateElement 调用,您就应该被设置
  • 我做到了,我在“CreateElement”中添加了相同的命名空间,但它也添加了 URL,例如:每个元素中的 xmlns = "URL"。

标签: c# xml xslt xml-parsing xmlhttprequest


【解决方案1】:

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

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

将您对HotelAvail 和孩子的CreateElement 呼叫更改为:

XMLDoc1.CreateElement("HotelAvail", WKSP_NS);

请参阅this fiddle 以获得工作演示。

顺便说一句,我同意 Jon Skeet 的观点,即您应该研究 LINQ to XML 而不是使用旧的 XmlDocument API。使用起来更好。

【讨论】:

    猜你喜欢
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多