如何删除 xmlns 标签

本文关键字:xmlns 标签 删除 何删除 | 更新日期: 2023-09-27 18:33:31

我有下面的代码。

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>

我该怎么办.谢谢。

如何删除 xmlns 标签

检查这个答案并尝试递归函数:

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