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.
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;
}