简体   繁体   中英

Load an object from an XML with a namespace

I have looked around and have not been able to implement a solutions for what I need. I apologize if this seems like a repeat questions.

I'm querying an IDOL service and need to load an object with from the xml response.

Give the sample xml:

<autnresponse xslns:autn="http://schemas.autonomy.com/aci/">
  <action>QUERY</action>
  <response>SUCCESS</response>
  <responsedata>
    <autn:numhits>2</autn:numhits>
    <autn:totalhits>2</autn:totalhits>
    <autn:totaldbdocs>2</autn:totaldbdocs>
    <autn:totaldbsecs>2</autn:totaldbsecs>
    <autn:hit>
      <autn:reference>http://blah</autn:reference>
      <autn:title>my title</autn:title>
    </autn:hit>
    <autn:hit>
      <autn:reference>http://blah</autn:reference>
      <autn:title>my title</autn:title>
    </autn:hit>        
  </responsedata>
</autnresponse>

I'm trying to load a list of a custom object with linq to XML.

Here is what I have tried as code, and I'm always getting no results.

 var namespaceManager = new XmlNamespaceManager(new NameTable());
 namespaceManager.AddNamespace("autn", "http://schemas.autonomy.com/aci/");

... Load Xelemet.....

IEnumerable<XElement> urls = raw.Elements(IDOLModule.GetNamespace()+ "hit");
            foreach (var single in urls)
            {
                var t = new url();
                t.Title="";
                t.Url="";
                listURL.Add(t);
            }

The urls variable is always coming back empty. I just need to target the autn:hits nodes and load their data to the object.

Cheers,

You could try something like that:

XElement doc = XElement.Parse(@"<autnresponse xmlns:autn=""http://schemas.autonomy.com/aci/"">
                                  <action>QUERY</action>
                                  <response>SUCCESS</response>
                                  <responsedata>
                                    <autn:numhits>2</autn:numhits>
                                    <autn:totalhits>2</autn:totalhits>
                                    <autn:totaldbdocs>2</autn:totaldbdocs>
                                    <autn:totaldbsecs>2</autn:totaldbsecs>
                                    <autn:hit>
                                      <autn:reference>http://blah</autn:reference>
                                      <autn:title>my title</autn:title>
                                    </autn:hit>
                                    <autn:hit>
                                      <autn:reference>http://blah</autn:reference>
                                      <autn:title>my title</autn:title>
                                    </autn:hit>
                                  </responsedata>
                                </autnresponse>");
XNamespace ns = ("http://schemas.autonomy.com/aci/");
//Console.WriteLine(doc.Descendants(ns + "numhits").First().Value);
IEnumerable<XElement> hits=doc.Descendants(ns + "hit");
foreach (XElement h in hits){
    Console.WriteLine(String.Format("{0} - {1}",h.Name.ToString(),h.Elements().First().Value));
};

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