0

i am trying to load the xml file as below and read the value for each element but the Result count always = Zero , below my xml example with code

1 ) My xml is :

<ROOT xmlns="authenticateUser">
<PARTYCODE></PARTYCODE>
<VRETCODE>10</VRETCODE>
<PRETCODE>10</PRETCODE>
<VRETERR>Incorrect user name or password entered.</VRETERR>
</ROOT>

2 ) My Code is :

 XDocument Doc = XDocument.Parse(strFileData);
                    var Result = (from Root in Doc.Descendants("ROOT")
                                  select new
                                  {
                                      PARTYCODE = Root.Element("PARTYCODE").Value ?? string.Empty,
                                      VRETCODE = Root.Element("VRETCODE").Value ?? string.Empty,
                                      PRETCODE = Root.Element("PRETCODE").Value ?? string.Empty,
                                      VRETERR = Root.Element("VRETERR").Value ?? string.Empty,
                                  }).ToList();
2
  • possible duplicate of Query Xml File for Records using Linq Commented Feb 17, 2015 at 6:33
  • Nothing to do with Classic ASP. Commented Feb 17, 2015 at 9:49

1 Answer 1

1

ROOT and all child elements are in the namespace authenticateUser, thanks to the xmlns="authenticateUser" default namespace attribute. So you need to query on on the the element local name plus the namespace name, which can be constructed using XName.Get(string, string)

    XDocument Doc = XDocument.Parse(strFileData);
    var Result = (from Root in Doc.Descendants(XName.Get("ROOT", "authenticateUser"))
                  select new
                  {
                      PARTYCODE = Root.Element(XName.Get("PARTYCODE", "authenticateUser")).Value ?? string.Empty,
                      VRETCODE = Root.Element(XName.Get("VRETCODE", "authenticateUser")).Value ?? string.Empty,
                      PRETCODE = Root.Element(XName.Get("PRETCODE", "authenticateUser")).Value ?? string.Empty,
                      VRETERR = Root.Element(XName.Get("VRETERR", "authenticateUser")).Value ?? string.Empty,
                  }).ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.