0

I have been trying to read data from an xml with "XPathNavigator" and with "XPathNodeIterator". In one type of file I succeeded and got what I wanted.

Successful example

XPathDocument docNav = new XPathDocument(file);
XPathNavigator nav;
nav = docNav.CreateNavigator();
XPathNodeIterator iterator;

List<ResultList> resultLists = new List<ResultList>();

iterator = nav.Select("/TestData/E2ETest/PROJECTNAME");
iterator.MoveNext();
string projectName = iterator.Current.Value;
//Console.WriteLine(iterator.Current.Value);

iterator = nav.Select("/TestData/E2ETest/TESTTYPE");
iterator.MoveNext();
string testType = iterator.Current.Value;

Xml File:

    <?xml version="1.0" encoding="utf-8"?>
    <TestData>
      <E2ETest>
        <PROJECTNAME>94446\FNReflectedXSSinoutputText</PROJECTNAME>
        <TESTTYPE>QUERY</TESTTYPE>
        <TESTOBJECTIVE>1017</TESTOBJECTIVE>
        <LANGUAGE>Apex</LANGUAGE>
        <RESULTS>1</RESULTS>
        <RESULTSLIST>
          <RESULT 
            InitialFileName="\test.apex" 
            InitialName="get" 
            InitialLine="2" 
            InitialColumn="67" 
            InitialLength="3" 
            InitialNodeId="241" 
            EndFileName="\testpage.apexp" 
            EndName="_ViewOutput" 
            EndLine="4" 
            EndColumn="44" 
            EndLength="11" 
            EndNodeId="311" />
        </RESULTSLIST>
        <QAANNOTATION></QAANNOTATION>
        <USEDEFAULTCONFIG>False</USEDEFAULTCONFIG>
        <SKIP>False</SKIP>
        <VERSIONNUMBER>9.3.0</VERSIONNUMBER>
        <DESCRIPTION></DESCRIPTION>
        <PBIBUGID>94446</PBIBUGID>
        <TAGS></TAGS>
      </E2ETest>
    </TestData>

Now with the same approach I don't get what I want, because I get all the values from the xml.

Failed example:

XPathDocument docNavigator = new XPathDocument(target);
XPathNavigator navigator;
navigator = docNavigator.CreateNavigator();
XPathNodeIterator iteratorFinal;

List<ResultList> resultLists = new List<ResultList>();

iteratorFinal = navigator.Select("/QuerySchema/QueryDataElement/QueryId");
iteratorFinal.MoveNext();
string name = iteratorFinal.Current.Value;   

Xml file:

    <?xml version="1.0"?>
    <QuerySchema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="CxQuery.xsd">
      <QueryDataElement>
        <QueryId>1001</QueryId>
        <PackageId>1001</PackageId>
        <Name>Async_Future_Method_Inside_Loops</Name>
        <Source>CxList_</Source>
        <CWE>0</CWE>
        <CxDescriptionId>389</CxDescriptionId>
        <Comments />
        <Severity>1</Severity>
        <IsExecutable>true</IsExecutable>
        <IsEncrypted>false</IsEncrypted>
      </QueryDataElement>
      <GroupDataElement>
        <GroupId>1001</GroupId>
        <Name>Apex_Force_com_Code_Quality</Name>
        <IsReadOnly>false</IsReadOnly>
        <IsEncrypted>false</IsEncrypted>
        <Description />
        <Language>16</Language>
        <LanguageName>Apex</LanguageName>
        <PackageType>0</PackageType>
        <PackageTypeName>Cx</PackageTypeName>
        <ProjectId>0</ProjectId>
      </GroupDataElement>
    </QuerySchema>

This file have extension cxq.

enter image description here

What am I doing wrong?

I was not aware of namespaces! I searched as instructed and still couldn't solve it. I still get all the value from the xml! I tried two different ways!

First:

XmlDocument document = new XmlDocument();
                document.Load(file);
        
                XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
                //manager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
                //manager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                manager.AddNamespace("", "CxQuery.xsd");
                XmlNodeList node = document.SelectNodes("/QuerySchema/QueryDataElement/QueryId", manager);

Second:

 XPathDocument xPathDocument = new XPathDocument(file);
                XPathNavigator navigator = xPathDocument.CreateNavigator();
                XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);
                ns.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
                ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                ns.AddNamespace("", "CxQuery.xsd");

                XPathExpression expression = XPathExpression.Compile("/QuerySchema/QueryDataElement/QueryId");
                expression.SetContext(ns);
                navigator.Evaluate(expression);

Thanks for the previous answers!

Finally, I was able to solve it!

  XmlDocument document = new XmlDocument();
                document.Load(file);
               
                XmlNodeList queryId = document.GetElementsByTagName("QueryId");
                string valueQueryIdXml = queryId[0].InnerText;
                if (valueQueryIdXml.Equals(idQuery))
                {
                    XmlNodeList queryNameXml = document.GetElementsByTagName("Name");
                    string valueQueryNameXml = queryNameXml[0].InnerText;
                    queryName = valueQueryNameXml;
                

}

Joao
  • 1
  • 2
  • 1
    `xmlns="CxQuery.xsd"` puts all the elements into a namespace named `CxQuery.csd`. You need to take that into account. With `XPathDocument` this is a fairly annoying affair involving `XmlNamespaceManager` and `XPathExpression.SetContext`. – Jeroen Mostert Apr 14 '22 at 13:53
  • @PeterB - the [second answer](https://stackoverflow.com/a/42079697/3744182) to [How to read an XPathDocument that has a namespace](https://stackoverflow.com/q/8363975/3744182) answers the question briefly. The first answer "Fix your XML" doesn't apply. – dbc Apr 14 '22 at 14:03

0 Answers0