加载有命名空间,但没有声名的XML

        public static void Sample1()
        { 
                String xmlString 
= @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">
    <soapenv:Header>
        <tns:RequestSOAPHeader xmlns:tns=""http://www.chinatelecom.com.cn/schema/ctcc/common/v2_1"">
            <tns:spId>22220001</tns:spId>
            <tns:spPassword>79A0FFF9D8CDC73F42691ABA88EB1195</tns:spPassword>
        </tns:RequestSOAPHeader>
    </soapenv:Header>
    <soapenv:Body>
        <ns2:sendSms xmlns:ns2=""http://www.chinatelecom.com.cn/schema/ctcc/sms/send/v2_1/local"">
            <ns2:addresses>tel:+8613156111754</ns2:addresses>
            <ns2:message>hello world</ns2:message>
        </ns2:sendSms>
    </soapenv:Body>
</soapenv:Envelope>
";

                XmlDocument xml 
= new XmlDocument();
                xml.LoadXml(xmlString);

                Console.WriteLine(xml.DocumentElement.Name);
        }

        
public static void Sample2()
        {
            String xmlString 
= @"<soapenv:Envelope>
    <soapenv:Header>
        <tns:RequestSOAPHeader>
            <tns:spId>22220001</tns:spId>
            <tns:spPassword>79A0FFF9D8CDC73F42691ABA88EB1195</tns:spPassword>
        </tns:RequestSOAPHeader>
    </soapenv:Header>
    <soapenv:Body>
        <ns2:sendSms>
            <ns2:addresses>tel:+8613156111754</ns2:addresses>
            <ns2:message>hello world</ns2:message>
        </ns2:sendSms>
    </soapenv:Body>
</soapenv:Envelope>
";

            NameTable nt 
= new NameTable();
            XmlNamespaceManager nsmgr 
= new XmlNamespaceManager(nt);
            nsmgr.AddNamespace(
"soapenv""http://schemas.xmlsoap.org/soap/envelope/");
            nsmgr.AddNamespace(
"tns""http://www.chinatelecom.com.cn/schema/ctcc/common/v2_1");
            nsmgr.AddNamespace(
"ns2""http://www.chinatelecom.com.cn/schema/ctcc/sms/send/v2_1/local");

            XmlParserContext context 
= new XmlParserContext(nt, nsmgr,  null,  XmlSpace.None);

            XmlDocument xml 
= new XmlDocument();
            
// Create the reader.
            using (XmlTextReader reader = new XmlTextReader(xmlString, XmlNodeType.Element, context))
            {
                xml.Load(reader);
            }

            Console.WriteLine(xml.DocumentElement.Name);


        }
原文地址:https://www.cnblogs.com/evlon/p/1408753.html