RaKer
RaKer

Reputation: 289

How to navigate to an element using XDocument using C#

I have an XML file that I need to read and I having trouble moving to a specific element to then start parsing over child elements. The element that I need to start reading from is <MenuCodes>, what is the best method of navigating to the element and then start reading?

Sample XML file

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Header>
      <ActivityId CorrelationId="8eb2de20-8dff-4460-ffff-866ac948dfab" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">b921a5f3-8188-4021-9c6f-416bdf78f629</ActivityId>
   </s:Header>
   <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <GetMenusResponse xmlns="http://www...">
         <GetMenusResult>
            <Header xmlns="http://www...">
               <SessionId>{8CCCED2E-3D44-4200-BBAC-66660CA08692}</SessionId>
               <CorrelationId>{AFFED12-AD36-4A2C-99FE-5E8D7CBC681D}</CorrelationId>
               <TimeStamp>2018-09-14T11:36:21.5123749Z</TimeStamp>
            </Header>
            <ResponseStatus xmlns="http://www...">
               <Status>OK</Status>
            </ResponseStatus>
            <ResponseBody xmlns="http://www...">
               <Refreshed>2018-09-14T11:36:22.0115845Z</Refreshed>
               <Menus>
                  <MenuCodes>
                     <MenuCode Code="AAA/A1" ShortText="Foo">
                        <ValidPrefix Prefix="V" />
                        <ValidPrefix Prefix="D" />
                        <ValidPrefix Prefix="N" />
                     </MenuCode>
                     <MenuCode Code="AAA/A2" ShortText="Foo2">
                        <ValidPrefix Prefix="D" />
                        <ValidPrefix Prefix="N" />
                     </MenuCode>
                   </MenuCodes>
               </Menus>
            </ResponseBody>
         </GetMenusResult>
      </GetMenusResponse>
   </s:Body>
</s:Envelope>   

Upvotes: 0

Views: 1633

Answers (2)

jdweng
jdweng

Reputation: 34429

The namespaces are an issue. So don't use namespace. See one line code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication68
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement menuCodes = doc.Descendants().Where(x => x.Name.LocalName == "MenuCodes").FirstOrDefault();
        }
    }
}

Upvotes: 0

DavidG
DavidG

Reputation: 119017

Here's a really verbose way of getting to that element, the important bit that most newcomers forget here is to add in the namespace:

var doc = XDocument.Parse(xml);
var soapNs = XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/");
var otherNs = XNamespace.Get("http://www...");
var menuCodes = doc
    .Element(soapNs + "Envelope")
    .Element(soapNs + "Body")
    .Element(otherNs + "GetMenusResponse")
    .Element(otherNs + "GetMenusResult")
    .Element(otherNs + "ResponseBody")
    .Element(otherNs + "Menus")
    .Element(otherNs + "MenuCodes");

Upvotes: 4

Related Questions