Best NBi code snippet using NBi.Core.DataSerialization.Flattening.Xml.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 NBi.Core.DataSerialization.Flattening.Xml;2using System.Xml;3using System.Xml.XPath;4{5 public static void Main()6 {7 XPathDocument document = new XPathDocument("books.xml");8 XPathNavigator navigator = document.CreateNavigator();9 XPathExpression expression = navigator.Compile("/bookstore/book[price>35.00]/title");10 XPathNodeIterator iterator = navigator.Select(expression);11 while (iterator.MoveNext())12 {13 Console.WriteLine(iterator.Current.Value);14 }15 }16}17boolean()18ceiling()19concat()20contains()21count()22ends-with()23floor()24format-number()25generate-id()26id()27lang()28last()29local-name()30name()31namespace-uri()32normalize-space()33not()34number()35position()36round()37starts-with()38string()39string-length()40sum()
XPathEngine
Using AI Code Generation
1var engine = new XPathEngine();2var result = engine.Evaluate("/root/child1/child2", doc);3foreach(var node in result)4{5 Console.WriteLine(node.OuterXml);6}
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!!