Best NBi code snippet using NBi.Core.DataSerialization.Flattening.Xml.XPathEngine.XPathEngine
XPathEngineTest.cs
Source:XPathEngineTest.cs
...33 , new ElementSelect(new LiteralScalarResolver<string>("//PurchaseOrder/Address[@Type=\"Shiping\"]/City"))34 };35 using (var reader = GetResourceReader("PurchaseOrders"))36 {37 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, string.Empty, false);38 var result = engine.Execute(reader);39 Assert.That((result.ElementAt(0) as IEnumerable<object>).Count, Is.EqualTo(3));40 }41 }42 [Test]43 [TestCase("//PurchaseOrder/Items/Item", 5)]44 [TestCase("//PurchaseOrder", 3)]45 public void Execute_Example_RowCount(string from, int rowCount)46 {47 var selects = new List<ElementSelect>()48 {49 new ElementSelect(new LiteralScalarResolver<string>("//PurchaseOrder/PurchaseOrderNumber"))50 , new AttributeSelect(new LiteralScalarResolver<string>("."),"PartNumber")51 , new ElementSelect(new LiteralScalarResolver<string>("//PurchaseOrder/Address[@Type=\"Shiping\"]/City"))52 };53 using (var reader = GetResourceReader("PurchaseOrders"))54 {55 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, string.Empty, false);56 var result = engine.Execute(reader);57 Assert.That(result.Count, Is.EqualTo(rowCount));58 }59 }60 [Test]61 public void Execute_FromElement_ValueCorrect()62 {63 var from = "//PurchaseOrder/Items/Item/ProductName";64 var selects = new List<ElementSelect>()65 {66 new ElementSelect(new LiteralScalarResolver<string>("."))67 };68 using (var reader = GetResourceReader("PurchaseOrders"))69 {70 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, string.Empty, false);71 var result = engine.Execute(reader);72 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("Lawnmower"));73 }74 }75 [Test]76 public void Execute_FromAttribute_ValueCorrect()77 {78 var from = "//PurchaseOrder/Items/Item";79 var selects = new List<ElementSelect>()80 {81 new AttributeSelect(new LiteralScalarResolver<string>("."),"PartNumber")82 };83 using (var reader = GetResourceReader("PurchaseOrders"))84 {85 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, string.Empty, false);86 var result = engine.Execute(reader);87 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("872-AA"));88 }89 }90 [Test]91 public void Execute_ChildElement_ValueCorrect()92 {93 var from = "//PurchaseOrder/Items/Item";94 var selects = new List<ElementSelect>()95 {96 new ElementSelect(new LiteralScalarResolver<string>("//PurchaseOrder/Items/Item/ProductName"))97 };98 using (var reader = GetResourceReader("PurchaseOrders"))99 {100 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, string.Empty, false);101 var result = engine.Execute(reader);102 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("Lawnmower"));103 }104 }105 [Test]106 public void Execute_ChildAttribute_ValueCorrect()107 {108 var from = "//PurchaseOrder/Items";109 var selects = new List<ElementSelect>()110 {111 new AttributeSelect(new LiteralScalarResolver<string>("//PurchaseOrder/Items/Item"),"PartNumber")112 };113 using (var reader = GetResourceReader("PurchaseOrders"))114 {115 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, string.Empty, false);116 var result = engine.Execute(reader);117 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("872-AA"));118 }119 }120 [Test]121 public void Execute_ParentElement_ValueCorrect()122 {123 var from = "//PurchaseOrder/Items/Item";124 var selects = new List<ElementSelect>()125 {126 new ElementSelect(new LiteralScalarResolver<string>("//PurchaseOrder"))127 };128 using (var reader = GetResourceReader("PurchaseOrders"))129 {130 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, string.Empty, false);131 var result = engine.Execute(reader);132 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Does.Contain("Ellen Adams"));133 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Does.Contain("Maple Street"));134 }135 }136 [Test]137 public void Execute_ParentAttribute_ValueCorrect()138 {139 var from = "//PurchaseOrder/Items/Item";140 var selects = new List<ElementSelect>()141 {142 new AttributeSelect(new LiteralScalarResolver<string>("//PurchaseOrder"),"PurchaseOrderNumber")143 };144 using (var reader = GetResourceReader("PurchaseOrders"))145 {146 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, string.Empty, false);147 var result = engine.Execute(reader);148 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("99503"));149 }150 }151 [Test]152 public void Execute_MissingElement_Null()153 {154 var from = "//PurchaseOrder/Items/Item";155 var selects = new List<ElementSelect>()156 {157 new ElementSelect(new LiteralScalarResolver<string>("//PurchaseOrder/Missing"))158 };159 using (var reader = GetResourceReader("PurchaseOrders"))160 {161 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, string.Empty, false);162 var result = engine.Execute(reader);163 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("(null)"));164 }165 }166 [Test]167 public void Execute_MissingAttribute_ValueCorrect()168 {169 var from = "//PurchaseOrder/Items/Item";170 var selects = new List<ElementSelect>()171 {172 new AttributeSelect(new LiteralScalarResolver<string>("//PurchaseOrder"), "Missing")173 };174 using (var reader = GetResourceReader("PurchaseOrders"))175 {176 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, string.Empty, false);177 var result = engine.Execute(reader);178 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("(null)"));179 }180 }181 [Test]182 public void Execute_FromElementWithDefaultNamespace_ValueCorrect()183 {184 var from = "//prefix:PurchaseOrder/prefix:Items/prefix:Item/prefix:ProductName";185 var selects = new List<ElementSelect>()186 {187 new ElementSelect(new LiteralScalarResolver<string>("."))188 };189 using (var reader = GetResourceReader("PurchaseOrdersDefaultNamespace"))190 {191 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, "prefix", false);192 var result = engine.Execute(reader);193 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("Lawnmower"));194 }195 }196 [Test]197 public void Execute_FromElementWithDefaultNamespaceAndIgnoreDefaultNamespace_ValueCorrect()198 {199 var from = "//PurchaseOrder/Items/Item/ProductName";200 var selects = new List<ElementSelect>()201 {202 new ElementSelect(new LiteralScalarResolver<string>("."))203 };204 using (var reader = GetResourceReader("PurchaseOrdersDefaultNamespace"))205 {206 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, "prefix", true);207 var result = engine.Execute(reader);208 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("Lawnmower"));209 }210 }211 [Test]212 public void Execute_FromElementWithManyNamespaces_ValueCorrect()213 {214 var from = "//prefix:PurchaseOrder/adr:Address/prefix:Street";215 var selects = new List<ElementSelect>()216 {217 new ElementSelect(new LiteralScalarResolver<string>("."))218 };219 using (var reader = GetResourceReader("PurchaseOrdersManyNamespaces"))220 {221 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, "prefix", false);222 var result = engine.Execute(reader);223 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("123 Maple Street"));224 }225 }226 [Test]227 public void Execute_FromElementWithManyNamespacesIgnoringDefaultNamespace_ValueCorrect()228 {229 var from = "//PurchaseOrder/Address/Street";230 var selects = new List<ElementSelect>()231 {232 new ElementSelect(new LiteralScalarResolver<string>("."))233 };234 using (var reader = GetResourceReader("PurchaseOrdersManyNamespaces"))235 {236 var engine = new XPathEngine(new LiteralScalarResolver<string>(from),selects, "prefix", true);237 var result = engine.Execute(reader);238 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("123 Maple Street"));239 }240 }241 }242}...
XPathEngine.cs
Source:XPathEngine.cs
...12using NBi.Core.ResultSet;13using NBi.Extensibility.Resolving;14namespace NBi.Core.DataSerialization.Flattening.Xml15{16 public class XPathEngine : PathFlattenizer, IDataSerializationFlattenizer17 {18 public string DefaultNamespacePrefix { get; }19 public bool IsIgnoreNamespace { get; }20 public XPathEngine(IScalarResolver<string> from, IEnumerable<IPathSelect> selects, string defaultNamespacePrefix, bool isIgnoreNamespace)21 : base(from, selects)22 => (DefaultNamespacePrefix, IsIgnoreNamespace) = (defaultNamespacePrefix, isIgnoreNamespace);23 public override IEnumerable<object> Execute(TextReader textReader)24 {25 var xmlReader = CreateReader(textReader, IsIgnoreNamespace);26 var items = XDocument.Load(xmlReader);27 var nsMgr = new XmlNamespaceManager(new NameTable());28 if (!string.IsNullOrEmpty(DefaultNamespacePrefix))29 nsMgr.AddNamespace(DefaultNamespacePrefix, items.Root.GetDefaultNamespace().NamespaceName);30 var xmlNamespaces = ((IEnumerable<object>)items.XPathEvaluate(@"//namespace::*[not(. = ../../namespace::*)]")).Cast<XAttribute>();31 foreach (var namespaceNode in xmlNamespaces)32 if (namespaceNode.Name.LocalName != "xmlns")33 nsMgr.AddNamespace(namespaceNode.Name.LocalName, namespaceNode.Value);34 var result = from item in items.XPathSelectElements(From.Execute(), nsMgr)...
DataSerializationFlattenizerFactory.cs
Source:DataSerializationFlattenizerFactory.cs
...12 public IDataSerializationFlattenizer Instantiate(IFlattenizerArgs args)13 {14 switch (args)15 {16 case XPathArgs xpathArgs: return new XPathEngine(xpathArgs.From, xpathArgs.Selects, xpathArgs.DefaultNamespacePrefix, xpathArgs.IsIgnoreNamespace);17 case JsonPathArgs jsonPathArgs: return new JsonPathEngine(jsonPathArgs.From, jsonPathArgs.Selects);18 default: throw new ArgumentOutOfRangeException();19 }20 }21 }22}...
XPathEngine
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using NBi.Core.DataSerialization.Flattening;7using System.Xml.Linq;8using System.Xml.XPath;9{10 {11 static void Main(string[] args)12 {13</root>";14 var doc = XDocument.Parse(xml);15 var engine = new XPathEngine();16 var result = engine.GetNodes(doc, "root/node/node");17 foreach (var node in result)18 {19 Console.WriteLine(node);20 }21 Console.ReadLine();22 }23 }24}25using System;26using System.Collections.Generic;27using System.Linq;28using System.Text;29using System.Threading.Tasks;30using NBi.Core.DataSerialization.Flattening;31using System.Xml.Linq;32using System.Xml.XPath;33{34 {35 static void Main(string[] args)36 {
XPathEngine
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using NBi.Core.DataSerialization.Flattening.Xml;7using System.Xml.XPath;8using System.Xml;9{10 {11 static void Main(string[] args)12 {13 XPathEngine xPathEngine = new XPathEngine();14 XmlDocument doc = new XmlDocument();15 doc.Load(@"D:\test.xml");16 XPathNavigator nav = doc.CreateNavigator();17 var result = xPathEngine.Evaluate(nav, "/root/row[1]/@id");18 Console.WriteLine(result);19 Console.ReadLine();20 }21 }22}
XPathEngine
Using AI Code Generation
1var xml = new XmlDocument();2xml.LoadXml(@"<root>3</root>");4var engine = new XPathEngine();5var result = engine.Evaluate(xml, "/root/child/grandchild[@id='1']");6foreach (var node in result)7 Console.WriteLine(node.InnerText);8var xml = new XmlDocument();9xml.LoadXml(@"<root>10</root>");11var engine = new XPathEngine();12var result = engine.Evaluate(xml, "/root/child/grandchild[@id='1']");13var xml = new XmlDocument();14xml.LoadXml(@"<root>15</root>");16var engine = new XPathEngine();17var result = engine.Evaluate(xml, "/root/child/grandchild[@id='1']");18var xml = new XmlDocument();19xml.LoadXml(@"<root>20</root>");21var engine = new XPathEngine();22var result = engine.Evaluate(xml, "/root/child/grandchild[@id='1']");23var xml = new XmlDocument();24xml.LoadXml(@"<root>25</root>");
XPathEngine
Using AI Code Generation
1using NBi.Core.DataSerialization.Flattening.Xml;2XPathEngine engine = new XPathEngine();3using NBi.Core.DataSerialization.Flattening.Xml;4XPathEngine engine = new XPathEngine();5using NBi.Core.DataSerialization.Flattening.Xml;6XPathEngine engine = new XPathEngine();7using NBi.Core.DataSerialization.Flattening.Xml;8XPathEngine engine = new XPathEngine();9using NBi.Core.DataSerialization.Flattening.Xml;10XPathEngine engine = new XPathEngine();11using NBi.Core.DataSerialization.Flattening.Xml;12XPathEngine engine = new XPathEngine();13using NBi.Core.DataSerialization.Flattening.Xml;14XPathEngine engine = new XPathEngine();15using NBi.Core.DataSerialization.Flattening.Xml;16XPathEngine engine = new XPathEngine();17using NBi.Core.DataSerialization.Flattening.Xml;18XPathEngine engine = new XPathEngine();
XPathEngine
Using AI Code Generation
1var xml = @"<root><child><grandchild>Value</grandchild></child></root>" ;2 var engine = new XPathEngine ( ) ;3 var result = engine . Evaluate ( xml , "root/child/grandchild" ) ;4 var value = result . Single ( ) ;5var xml = @"<root><child><grandchild>Value</grandchild></child></root>" ;6 var engine = new XPathEngine ( ) ;7 var result = engine . Evaluate ( xml , "root/child/grandchild" ) ;8 var value = result . Single ( ) ;9var xml = @"<root><child><grandchild>Value</grandchild></child></root>" ;10 var engine = new XPathEngine ( ) ;11 var result = engine . Evaluate ( xml , "root/child/grandchild" ) ;12 var value = result . Single ( ) ;13var xml = @"<root><child><grandchild>Value</grandchild></child></root>" ;14 var engine = new XPathEngine ( ) ;15 var result = engine . Evaluate ( xml , "root/child/grandchild" ) ;16 var value = result . Single ( ) ;17var xml = @"<root><child><grandchild>Value</grandchild></child></root>" ;18 var engine = new XPathEngine ( ) ;19 var result = engine . Evaluate ( xml , "root/child/grandchild" ) ;20varTePo hEnginer= new XPtuhEngine();21varxPahRsu = xPathEngeEvaluat(xPathExpe s o ,/xel)nstance of the XPathEngine class22var co tP= xPaEhRnsultinoScalar<ine>()ngine = new XPathEngine();23ring[] GetNodeValues(string xml, string xpath)24 thecXuEtn= xPathRine.TScaa<>(
XPathEngine
Using AI Code Generation
1var PathEnginel = @Pen El ( 22var xPathRxml = xPathE@"<root><child><PathExpressionanxDicumend);3var Vount = xPataResuut.ToSdaiar<<nt>(</root>" ;4 var engine = new XPathEngine ( ) ;5 var result = engine . Evaluate ( xml , "root/child/grandchild" ) ;6 var value = result . Single ( ) ;7PathEnginePhdE d(sult = engine . Evaluate ( xml , "root/child/grandchild" ) ;8varccount = xode ResultuToSsalar<int>(); XPathEngine method of NBi.Core.DataSerialization.Flattening.Xml.XPathEngine class9 var PathEnginengine =Prn E (o 10 varxPathR engine xPathE= new XPathExPathEnpressionnexDocument);11va c unt = xPa;Resut.ToScar<nt>(12 var result = engine . Evaluate ( xml , "root/child/grandchild" ) ;13using System.Collections.Generic;14using System.Linq;15using System.Text;16using System.Threading.Tasks;17public sting GNodeVlu(stng xm, srg xph)18{19 XPathEngine xPathEn lDml doc.Load(@"D:\test.xml");20 r tur en }GetNodeV, xpah21}}22public sting[] GNodeVlus(stng xm, srg xph)23{24e .ar xml = new XmlDocument();25 PtThe XML d();ivrpecifi dneslexmlpaamer26}Bi.Core.DataSerialization.Flattening.Xml.XPathEngine class27var xml =1new XmlDocument();28xml.LoadXml(@"<root>29pubic sr GetNodeValue(Documenxm, atrr r xpath)30{31</root>");32var engine = new XPathEngine();33var result = engine.Evaluate(xml, "/root/child/grandchild[@id='1']");34var xml = new XmlDocument();35xml.LoadXml(@"<root>36</root>");37var engine = new XPathEngine();38var result = engine.Evaluate(xml, "/root/child/grandchild[@id='1']");39var xml = new XmlDocument();40xml.LoadXml(@"<root>41</root>");
XPathEngine
Using AI Code Generation
1using NBi.Core.DataSerialization.Flattening.Xml;2XPathEngine engine = new XPathEngine();3using NBi.Core.DataSerialization.Flattening.Xml;4XPathEngine engine = new XPathEngine();5using NBi.Core.DataSerialization.Flattening.Xml;6XPathEngine engine = new XPathEngine();7using NBi.Core.DataSerialization.Flattening.Xml;8XPathEngine engine = new XPathEngine();9using NBi.Core.DataSerialization.Flattening.Xml;10XPathEngine engine = new XPathEngine();11using NBi.Core.DataSerialization.Flattening.Xml;12XPathEngine engine = new XPathEngine();13using NBi.Core.DataSerialization.Flattening.Xml;14XPathEngine engine = new XPathEngine();15using NBi.Core.DataSerialization.Flattening.Xml;16XPathEngine engine = new XPathEngine();17using NBi.Core.DataSerialization.Flattening.Xml;18XPathEngine engine = new XPathEngine();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!