要从 C# WebService 的返回报文中去除 XML 属性,可以使用 XmlDocument 类来解析和修改 XML 文档。以下是一个示例代码,说明如何去除 XML 属性:

using System;
using System.Xml;

public class Program
{
    public static void Main()
    {
        // 假设返回的报文是一个 XML 字符串
        string xmlString = '<root><element attribute1='value1' attribute2='value2'>content</element></root>';

        // 创建一个 XmlDocument 对象并加载 XML 字符串
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlString);

        // 获取根节点
        XmlNode root = xmlDoc.DocumentElement;

        // 获取要去除属性的节点
        XmlNode element = root.SelectSingleNode("element");

        // 移除指定的属性
        XmlAttribute attributeToRemove = element.Attributes["attribute1"];
        element.Attributes.Remove(attributeToRemove);

        // 获取修改后的 XML 字符串
        string modifiedXmlString = xmlDoc.OuterXml;
        Console.WriteLine(modifiedXmlString);
    }
}

在上面的示例中,我们首先创建一个 XmlDocument 对象,并使用 LoadXml 方法加载 XML 字符串。然后,我们使用 SelectSingleNode 方法获取要去除属性的节点,并使用 Attributes 属性获取该节点的属性集合。我们可以使用 Remove 方法从属性集合中移除指定的属性。最后,我们使用 OuterXml 属性获取修改后的 XML 字符串。

请注意,上面的示例假设要去除的属性已知,并且 XML 结构已知。如果属性是动态的或者 XML 结构不确定,您可能需要根据具体情况进行调整。

C# WebService 返回报文去除 XML 属性方法

原文地址: https://www.cveoy.top/t/topic/p7od 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录