Home > Net >  Consult linq to XML parsing the SOAP XML string
Consult linq to XML parsing the SOAP XML string

Time:04-18

I found a circle on the Internet, I found the SOAP XML string parsing method is done with xmldocument,
But don't want to use accustomed to linq to XML use xmldocument, could you tell me how to resolve this use xdocument? Thank you very much!

 & lt; ? The XML version="1.0" encoding="utf-8"?> 


1 ai6nf4h120a11303 & lt;/code>
10.0 & lt;/length>


1 ai6nf4h120b11303 & lt;/code>
20.0 & lt;/length>


1 ai4nf4h020bhh803 & lt;/code>
0.0 & lt;/length>

<:/ns2 getMaterialListResponse>

CodePudding user response:

Demo:
 
Public Map The map=new ConcurrentHashMap (a);

Public Map parse (String soap) throws DocumentException {
Document doc=DocumentHelper. ParseText (soap);//message to doc object
Element root=doc. GetRootElement ();//get the root element, to parse the XML tree recursive
GetCode (root);
return map;
}

Public void getCode Element (root) {
If (root. Elements ()!=null) {
List List=root. Elements ();//if the current node has child nodes, and find a child node
For (Element e: list) {//traverse each node
If (e.e lements (). The size () & gt; 0 {
GetCode (e);//the current node is not null, recursive traversal child nodes;
}
If (e.e lements (). The size ()==0) {
The map. The put (um participant etName (), um participant etTextTrim ());
}//if the leaf node, then the name and value in a map directly to the
}
}
}
Public static void main (String [] args) {
String="soap StringBuilder sb=new StringBuilder ();
Sb. Append (" & lt; ? The XML version=\ \ "1.0" encoding=\ "utf-8 "? & gt;"
+ "& lt; The 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/\ "& gt;"
+ "& lt; Soap: Body> + "& lt; TheCityName>" + city
+ "& lt;/theCityName>
"
+ "& lt;/soap: Body> " );

//initialize the message, call the parse method, get the results map, and then field according to the requirements, or converted to other formats
The Map Map=parse (soap. The toString ());
//the field s: SourceSysId value;
String theCityName=map. Get (" theCityName "). The toString ();
System. The out. Println (" theCityName "+ theCityName);

}


  •  Tags:  
  • C#
  • Related