1

This is mycode. I want to get elements in string xml but i didn't. Please help me.

$

   string xml = "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><GetCustomerInfoResponse xmlns='http://tempuri.org/'><GetCustomerInfoResult xmlns:a='http://schemas.datacontract.org/2004/07/PaymentGatewayEVNSPC' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><a:Address>137 Phường Tân Xuyên</a:Address><a:Bills><a:BillInfo><a:Amount>1516682</a:Amount><a:BillCode>170810TD</a:BillCode><a:DenNgay i:nil='true'></a:DenNgay><a:HoaDonID>33045487</a:HoaDonID><a:Month>8</a:Month><a:SoHo>0</a:SoHo><a:TuNgay i:nil='true'></a:TuNgay><a:TyLeGia i:nil='true'></a:TyLeGia><a:Year>2017</a:Year></a:BillInfo><a:BillInfo><a:Amount>89000</a:Amount><a:BillCode>170810DC</a:BillCode><a:DenNgay i:nil='true'></a:DenNgay><a:HoaDonID>33045487</a:HoaDonID><a:Month>8</a:Month><a:SoHo>0</a:SoHo><a:TuNgay i:nil='true'></a:TuNgay><a:TyLeGia i:nil='true'></a:TyLeGia><a:Year>2017</a:Year></a:BillInfo><a:BillInfo><a:Amount>148028</a:Amount><a:BillCode>170810VC</a:BillCode><a:DenNgay i:nil='true'></a:DenNgay><a:HoaDonID>33045488</a:HoaDonID><a:Month>8</a:Month><a:SoHo>0</a:SoHo><a:TuNgay i:nil='true'></a:TuNgay><a:TyLeGia i:nil='true'></a:TyLeGia><a:Year>2017</a:Year></a:BillInfo></a:Bills><a:CustomerCode>PB14010040801</a:CustomerCode><a:DanhSo>44900L36</a:DanhSo><a:MaSoThue></a:MaSoThue><a:MaTram></a:MaTram><a:Name>Cơ Sở Mộc Thành Tài</a:Name><a:NganhNghe></a:NganhNghe><a:PhienGCS></a:PhienGCS><a:Session></a:Session><a:SoCongTo>14226305</a:SoCongTo><a:SoGhiChiSo>A1010D001</a:SoGhiChiSo></GetCustomerInfoResult></GetCustomerInfoResponse></s:Body></s:Envelope>";

window.jQuery

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

        var ns = new XmlNamespaceManager(new NameTable());
       // XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);
        ns.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
        // ns.AddNamespace("xmlns","'http://tempuri.org/'");
        ns.AddNamespace("a", "http://schemas.datacontract.org/2004/07/PaymentGatewayEVNSPC");
        ns.AddNamespace("i", "http://www.w3.org/2001/XMLSchema-instance");


        string xpath = "s:Envelope/s:Body/GetCustomerInfoResponse/GetCustomerInfoResult";
        // string xpath = "myDataz/listS/sog";
        var nodes = xmlDoc.SelectNodes(xpath,ns);
        ;

        foreach (XmlNode childrenNode in nodes)
        {
            Console.Write(childrenNode.SelectSingleNode("a:Address",ns).InnerXml);
            Console.Write("\n");
        }
5
  • Remove "s:" and "a:" from xml string. Commented Jun 4, 2018 at 8:44
  • possible duplicate stackoverflow.com/questions/18230605/… Commented Jun 4, 2018 at 8:52
  • An explicit question may help in clearing up what the problem is. Commented Jun 4, 2018 at 9:14
  • This kind of work is easier with XDocument , are you tied to using XmlDocument? Commented Jun 4, 2018 at 9:14
  • Sorry i am new member ... this is my code and it not working .i want to get elements in string xml but i didn't .please help me Commented Jun 4, 2018 at 9:23

1 Answer 1

0

Unfortunately, in a namespaced document, you can't use XPath with the default namespace. See this answer for more info.

This works:

var ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
ns.AddNamespace("a", "http://schemas.datacontract.org/2004/07/PaymentGatewayEVNSPC");
ns.AddNamespace("i", "http://www.w3.org/2001/XMLSchema-instance");
//Add a prefix for the default namespace for GetCustomerInfoResponse and GeteCustomerInforResult
ns.AddNamespace("t","http://tempuri.org/");

string xpath = "s:Envelope/s:Body/t:GetCustomerInfoResponse/t:GetCustomerInfoResult";
var nodes = xmlDoc.SelectNodes(xpath, ns);

foreach (XmlNode childrenNode in nodes)
{
    Console.Write(childrenNode.SelectSingleNode("a:Address", ns).InnerXml);
    Console.Write("\n");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.