如何删除xmlns标记

时间:2016-02-06 10:09:24

标签: c# .net soap attributes

我有以下代码。

namespace IrancellSmsServer
{

          [WebService(Namespace = "http://www.csapi.org/schema/parlayx/data/sync/v1_0/local")]

    public class SoapServer : System.Web.Services.WebService
    {

         [SoapDocumentMethod(Action = "",ParameterStyle = SoapParameterStyle.Bare)]


        [WebMethod]
        public syncOrderRelationResponse syncOrderRelation(
            Sync.UserID userID,
            string spID,
            string productID,
            string serviceID,
            string serviceList,
            int updateType,
            string updateTime,
            string updateDesc,
            string effectiveTime,
            string expiryTime,
            item[] extensionInfo
            )
        {


            syncOrderRelationResponse a = new syncOrderRelationResponse();
            a.result = 0;
            a.resultDescription = "OK";

            return a;
        }

为我生成这个:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <userID xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">
      <ID xmlns="">string</ID>
      <type xmlns="">int</type>
    </userID>
    <spID xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">string</spID>
    <productID xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">string</productID>
    <serviceID xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">string</serviceID>
    <serviceList xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">string</serviceList>
    <updateType xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">int</updateType>
    <updateTime xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">string</updateTime>
    <updateDesc xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">string</updateDesc>
    <effectiveTime xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">string</effectiveTime>
    <expiryTime xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">string</expiryTime>
    <extensionInfo xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">
      <item>
        <key xmlns="">string</key>
        <value xmlns="">string</value>
      </item>
      <item>
        <key xmlns="">string</key>
        <value xmlns="">string</value>
      </item>
    </extensionInfo>
  </soap:Body>
</soap:Envelope>

我希望删除这些xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local。我之前没事,直到我添加了这一行

 [SoapDocumentMethod(Action = "",ParameterStyle = SoapParameterStyle.Bare)]

我添加了这行后,我开始获得这个额外的命名空间;

我想要生成的是这样的:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <userID>
      <ID xmlns="">string</ID>
      <type xmlns="">int</type>
    </userID>
    <spID>string</spID>
    <productID>string</productID>
    <serviceID>string</serviceID>
    <serviceList>string</serviceList>
    <updateType>int</updateType>
    <updateTime>string</updateTime>
    <updateDesc>string</updateDesc>
    <effectiveTime>string</effectiveTime>
    <expiryTime>string</expiryTime>
    <extensionInfo>
      <item>
        <key xmlns="">string</key>
        <value xmlns="">string</value>
      </item>
      <item>
        <key xmlns="">string</key>
        <value xmlns="">string</value>
      </item>
    </extensionInfo>
  </soap:Body>
</soap:Envelope>
我应该怎么做。 感谢。

1 个答案:

答案 0 :(得分:0)

检查this answer并尝试递归函数:

//Implemented based on interface, not part of algorithm
public static string RemoveAllNamespaces(string xmlDocument)
{
    XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument));

    return xmlDocumentWithoutNs.ToString();
}

//Core recursion function
 private static XElement RemoveAllNamespaces(XElement xmlDocument)
    {
        if (!xmlDocument.HasElements)
        {
            XElement xElement = new XElement(xmlDocument.Name.LocalName);
            xElement.Value = xmlDocument.Value;

            foreach (XAttribute attribute in xmlDocument.Attributes())
                xElement.Add(attribute);

            return xElement;
        }
        return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el)));
    }