简体   繁体   中英

How to Linq2Xml a webservice?

I'm calling a WebService with HttpWebRequest.Create method and read it with StreamReader, (below method do this job):

private string ReadWebMethod(string address)
{
    var myRequest = (HttpWebRequest)HttpWebRequest.Create(address);
    myRequest.Method = "POST";
    using (var responseReader = new StreamReader(myRequest.GetResponse().GetResponseStream()))
        return responseReader.ReadToEnd();
}

This method works well and output a string like this:

<ArrayOfAppObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<AppObject>
<Name>MyApp</Name>
<Image>StoreApp.png</Image>
<Version>SM2.1.0</Version>
</AppObject>
</ArrayOfAppObject>

Now I want to have a look in this string, So I use XEle.net and parse the string. (below code):

XElement x = XElement.Parse(ReadWebMethod(address));

Now, When I foreach, x.Elements("AppObject"), it doesnt return any item and skip the foreach. My foreach is like this:

foreach (var item in x.Elements("AppObject"))
{
    listApplication.Add(new AppObject { Image = item.Element("Image").Value, Name = item.Element("Name").Value, Version = item.Element("Version").Value });
}

I create a local string and remove attributes after "ArrayOfAppObject" (xmlns:xsi="htt...)(some where name it xmlnamespace) and test it again, And it works as well and foreach does not skipped!

SO, How can I use the xml with namespace?

use XDocument class

using System.Xml.Linq;

//...
var xml = ReadWebMethod(address);
var xdocument = System.Xml.Linq.XDocument.Parse(xml);

updates

as your XML data define the namespace.. xmlns="http://tempuri.org/"

You must declare full XName with valid namespace. to access each element value

XName theElementName = XName.Get("AppObject", "http://tempuri.org/");
//or alternate way..
XName theElementName = XName.Get("{http://tempuri.org/}AppObject");

here 's sample test method

[TestMethod]
public void ParseXmlElement()
{
    XDocument xdoc = XDocument.Parse(this.mockXml);

    XName appTag = XName.Get("{http://tempuri.org/}AppObject");
    XName nameTag = XName.Get("{http://tempuri.org/}Name");
    XName imageTag = XName.Get("{http://tempuri.org/}Image");

    XElement objElement = xdoc.Root.Element(appTag);
    Assert.IsNotNull(objElement, "<AppObject> not found");
    Assert.AreEqual("{http://tempuri.org/}AppObject", objElement.Name);

    XElement nameElement = objElement.Element(nameTag);
    Assert.IsNotNull(nameElement, "<Name> not found");
    Assert.AreEqual("MyApp", nameElement.Value);

    XElement imageElement = objElement.Element(imageTag);
    Assert.IsNotNull(imageElement, "<Image> not found");
    Assert.AreEqual("StoreApp.png", imageElement.Value);
}

using Xml.Linq this way..

[TestMethod]
public void ParseXmlLinq()
{
    XDocument xdoc = XDocument.Parse(this.mockXml);

    XElement app = xdoc.Root.Elements()
        .FirstOrDefault(e => e.Name == XName.Get("AppObject", "http://tempuri.org/"));

    Assert.IsNotNull(app, "<AppObject> not found");

    XElement img = app.Elements()
        .FirstOrDefault(x => x.Name == XName.Get("Image", "http://tempuri.org/"));

    Assert.IsNotNull(img, "<Image> not found");
    Assert.AreEqual("StoreApp.png", img.Value);
}

Note that I just mock up and parse string from your XML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM