locked
get the value from xml RRS feed

  • Question

  • User-1834799348 posted

    hi ,can I get help getting value NetOUt come from  xml into vairiable sc.Below is the code  ,im doing unit  testing

    string s2 = "_NetOutcome";

    [Test]

    public void ValidateTest()

    {

    var o = new Program();

    var n = o.NewOrderRequest();

    string x = n.ToString();

    XDocument document = XDocument.Parse(x);//document  getting xml input

    var sc =document.XPathEvaluate("./OMAPIForEFW/SystemResult[@_NetOutcome]");

     

    // var sc = document.XPathSelectElement("./OMAPIForEFW/SystemResult[@_NetOutcome]").Value;

    //var sc = document.Descendants("OMAPIForEFW");

    // string sc = document.CreateNavigator().SelectSingleNode("/SystemResult/@_NetOutcome")?.ToString();

      Assert.AreEqual(s2,sc);

    xml

    <OEFW>
      <ResultOfValidateNew MTVFMEAm="" MTVBuyingPowerAm="" MTVSMAAm="" MTVMinOptEquityCallAm="" MTVCashMoneyFundsAm="" MTVNetActivityAm="" ETNInd="N" SPRating="" FitchRating="" ParentMoodyRating="" ParentSPRating="" ParentFitchRating="" PreferredSecurityDisclaimerInd="" PreferredSecurityInd="" ParentPreferredSecurityInd="" CalculatedLimitPrice="" EBSAccountType="" SeniorUnsecuredDebtInd="" MLSecurityNumber="40853" DOLLPRTrait="" TotMarkUpDown="" PercentPMP="" PMP="" CUSIP="478160104" RIC="JNJ.N" SEDOL="2475833" SettlementType="" PMEID="101000320" PMEInstrumentTradingLineId="1001" MPID="" BusinessDate="11/5/2019" SettlementDate="2019-11-07" GTDDate="" />
      <SystemResult _NetOutcome="BLOCK" _ExecutionContextId="{A8E97E7D-0455-4021-AF5F-82F604B8E6F8}" _MachineName="MPHEWDXCSV007">
        <ProblemList>
          <Problem _ErrorNumber="11113" _ErrorMessage="number is invalid." _OutcomeCode="BLOCK" _SRMCode="" _SRMErrorCode="" _SRMErrorMessage="" _ActivityId="NOTFOUND" _UsedProfile="0" _Priority="0" />
            </ProblemList>
      </SystemResult>
    </OEFW>

    Tuesday, November 5, 2019 6:53 PM

Answers

  • User283571144 posted

     Hi cvm321, 

    According to your codes and description,  I suggest you could use System.Xml library to load the domxml file and use the GetElementsByTagName method to get the nodelist object find the value of the child node.

    More details, you could refer to below codes:

    var xmlDoc = new XmlDocument();
                xmlDoc.Load(@"C:\Users\yijings\source\repos\WebApplication10\Demo\XMLFile1.xml");
                XmlNodeList logger = xmlDoc.GetElementsByTagName("SystemResult");
                string attrVal = logger[0].Attributes["_NetOutcome"].Value;
                Console.Write(attrVal);
    
    

    Result:

    Best Regards,

    Bradno

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 6, 2019 8:02 AM

All replies

  • User-1834799348 posted

    var sc = document.Element("NetOutCome").Value;//error Object reference not set to an instance of an object. 

    Tuesday, November 5, 2019 7:00 PM
  • User-1834799348 posted

    I was trying to do this

    Enumerable att = (IEnumerable)document.XPathEvaluate("./OMAPIForEFW/SystemResult");//nothing is showing in att when I debug

    IEnumerable<XAttribute> attList = att.Cast<XAttribute>();

    XAttribute att1 = attList.First();//
    Additional information: Unable to cast object of type 'System.Xml.Linq.XElement' to type 'System.Xml.Linq.XAttribute'.

    Assert.AreEqual(s2, att1);

    Tuesday, November 5, 2019 7:58 PM
  • User283571144 posted

     Hi cvm321, 

    According to your codes and description,  I suggest you could use System.Xml library to load the domxml file and use the GetElementsByTagName method to get the nodelist object find the value of the child node.

    More details, you could refer to below codes:

    var xmlDoc = new XmlDocument();
                xmlDoc.Load(@"C:\Users\yijings\source\repos\WebApplication10\Demo\XMLFile1.xml");
                XmlNodeList logger = xmlDoc.GetElementsByTagName("SystemResult");
                string attrVal = logger[0].Attributes["_NetOutcome"].Value;
                Console.Write(attrVal);
    
    

    Result:

    Best Regards,

    Bradno

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 6, 2019 8:02 AM
  • User-1834799348 posted

    Thank you

    Wednesday, November 6, 2019 4:10 PM