xml, c#

<SOAP:Envelope xmlns:SOAP="urn:schemas-xmlsoap-org:soap.v1">
<SOAP:Header></SOAP:Header><SOAP:Body>
<m:RetrieveResponse>
<RetrieveResult>
<Status>Completed</Status>
<Item>
<number>L899999</number>
<serviceLocationAddress>2201 XYZ STREET</serviceLocationAddress>
<serviceLocationCity>LONG BEACH</serviceLocationCity>
<serviceLocationState>CA</serviceLocationState>
<serviceLocationZip>90810</serviceLocationZip>
<customerName>FM COMMUNICATIONS</customerName>
<customerPhoneNo>7asdfasdf168322026</customerPhoneNo>
<customerFaxNo></customerFaxNo>
<modelNo>GYHD250CHU</modelNo>
<serialNumber>12345678</serialNumber>
<repairStatus>Received</repairStatus>
<customerReferenceInfo>TEST CUST REF#</customerReferenceInfo>
<productCategory>Pro HD</productCategory>
</Item>
<Item>
<number>L123456</number>
<serviceLocationAddress>2201 E ABC STREET</serviceLocationAddress>
<serviceLocationCity>LONG BEACH</serviceLocationCity>
<serviceLocationState>CA</serviceLocationState>
<serviceLocationZip>90810</serviceLocationZip>
<customerName>AM COMMUNICATIONS</customerName>
<customerPhoneNo>7168322026</customerPhoneNo>
<customerFaxNo></customerFaxNo>
<modelNo>GYHD4354250CHU</modelNo>
<serialNumber>12345678</serialNumber>
<repairStatus>Received</repairStatus>
<customerReferenceInfo>TEST CUST REF#</customerReferenceInfo>
<productCategory>Pro HD</productCategory>
</Item>
</RetrieveResult>
</m:RetrieveResponse></SOAP:Body></SOAP:Envelope>


MY C#/aspx application will receive above xml format. and I want to do two things.

1. I want to find how total count of <Item> inside of xml string. Remember I get it as string. not actually xml file.
2. I just want learn how to extract information. For example, I will have textbox to search by <number> and I want
to show servicelocationaddress and model no and etc. How can I do that?
LVL 1
ITsolutionWizardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi ITsolutionWizard;

First of all the soap message you posted is not a Well-Formed XML document. The node, <m:RetrieveResponse>, has a namespace, m:, that is not defined in the document before its use. This will cause options of using XDocument or XmlDocument objects from being used. So that leaves just using the String class to find what you are looking for. So what I did was to remove both namespace m: from both locations in the string so that I can load the string into an XML document and that is what is being shown below. Please verify this fact.
// The SOAP message as a string.
string soapMsg = "<SOAP:Envelope xmlns:SOAP=\"urn:schemas-xmlsoap-org:soap.v1\"><SOAP:Header></SOAP:Header><SOAP:Body><RetrieveResponse><RetrieveResult><Status>Completed</Status><Item><number>L899999</number><serviceLocationAddress>2201 XYZ STREET</serviceLocationAddress><serviceLocationCity>LONG BEACH</serviceLocationCity><serviceLocationState>CA</serviceLocationState><serviceLocationZip>90810</serviceLocationZip><customerName>FM COMMUNICATIONS</customerName><customerPhoneNo>7asdfasdf168322026</customerPhoneNo><customerFaxNo></customerFaxNo><modelNo>GYHD250CHU</modelNo><serialNumber>12345678</serialNumber><repairStatus>Received</repairStatus><customerReferenceInfo>TEST CUST REF#</customerReferenceInfo><productCategory>Pro HD</productCategory></Item><Item><number>L123456</number><serviceLocationAddress>2201 E ABC STREET</serviceLocationAddress><serviceLocationCity>LONG BEACH</serviceLocationCity><serviceLocationState>CA</serviceLocationState><serviceLocationZip>90810</serviceLocationZip><customerName>AM COMMUNICATIONS</customerName><customerPhoneNo>7168322026</customerPhoneNo><customerFaxNo></customerFaxNo><modelNo>GYHD4354250CHU</modelNo><serialNumber>12345678</serialNumber><repairStatus>Received</repairStatus><customerReferenceInfo>TEST CUST REF#</customerReferenceInfo><productCategory>Pro HD</productCategory></Item></RetrieveResult></RetrieveResponse></SOAP:Body></SOAP:Envelope>";

// Load the XML into a XDocument object
XDocument xdoc = XDocument.Parse(soapMsg);

// Get a List of XElement of node name of Item
var items = xdoc.Descendants("Item").ToList();

// Get the number of Item nodes in the SOAP message
Console.WriteLine("Total Item = {0}", items.Count());
// Display info from soap message
foreach (XElement item in items)
{
    foreach (XElement child in item.Elements())
    {
        Console.WriteLine("{0} = {1}", child.Name.LocalName, child.Value);
    }
    Console.WriteLine();
}

Open in new window


This is the output of the above code.
Total Item = 2
number = L899999
serviceLocationAddress = 2201 XYZ STREET
serviceLocationCity = LONG BEACH
serviceLocationState = CA
serviceLocationZip = 90810
customerName = FM COMMUNICATIONS
customerPhoneNo = 7asdfasdf168322026
customerFaxNo = 
modelNo = GYHD250CHU
serialNumber = 12345678
repairStatus = Received
customerReferenceInfo = TEST CUST REF#
productCategory = Pro HD

number = L123456
serviceLocationAddress = 2201 E ABC STREET
serviceLocationCity = LONG BEACH
serviceLocationState = CA
serviceLocationZip = 90810
customerName = AM COMMUNICATIONS
customerPhoneNo = 7168322026
customerFaxNo = 
modelNo = GYHD4354250CHU
serialNumber = 12345678
repairStatus = Received
customerReferenceInfo = TEST CUST REF#
productCategory = Pro HD

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.