Best NBi code snippet using NBi.Core.DataSerialization.Flattening.Xml.XPathEngine.Execute
XPathEngineTest.cs
Source:XPathEngineTest.cs
...22 var reader = new StreamReader(stream);23 return reader;24 }25 [Test]26 public void Execute_Example_ColumnCount()27 {28 var from = "//PurchaseOrder/Items/Item";29 var selects = new List<ElementSelect>()30 {31 new ElementSelect(new LiteralScalarResolver<string>("//PurchaseOrder/PurchaseOrderNumber"))32 , new AttributeSelect(new LiteralScalarResolver<string>("."), "PartNumber")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
...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)35 select GetObj(item, nsMgr);36 return result;37 }38 private object GetObj(XElement x, IXmlNamespaceResolver ns)39 {40 var obj = new List<object>();41 obj.AddRange(BuildXPaths(x, ns, Selects).ToArray());42 return obj;43 }44 protected internal IEnumerable<object> BuildXPaths(XElement item, IXmlNamespaceResolver ns, IEnumerable<IPathSelect> selects)45 {46 foreach (var select in selects)47 if (select is AttributeSelect)48 {49 var attributeSelect = select as AttributeSelect;50 yield return51 (52 (53 item.XPathSelectElement(attributeSelect.Path.Execute(), ns)54 ?? new XElement("null", "(null)")55 ).Attribute(attributeSelect.Attribute)56 ?? new XAttribute("null", "(null)")57 ).Value;58 }59 else if (select is EvaluateSelect)60 {61 yield return62 (63 item.XPathEvaluate(select.Path.Execute(), ns)64 ?? new XElement("null", "(null)")65 );66 }67 else68 yield return69 (70 item.XPathSelectElement(select.Path.Execute(), ns)71 ?? new XElement("null", "(null)")72 ).Value;73 }74 protected XmlReader CreateReader(TextReader textReader, bool isRemoveDefaultNamespace)75 {76 var settings = new XmlReaderSettings();77 if (isRemoveDefaultNamespace)78 return new XmlIgnoreNamespaceReader(textReader, settings);79 else80 return XmlReader.Create(textReader, settings);81 }82 }83}...
Execute
Using AI Code Generation
1using NBi.Core.DataSerialization.Flattening;2using NBi.Core.DataSerialization.Flattening.Xml;3using System;4using System.Collections.Generic;5using System.IO;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9using System.Xml;10{11 {12 static void Main(string[] args)13 {14 string xmlPath = @"C:\Users\user\Documents\Visual Studio 2015\Projects\ConsoleApp1\ConsoleApp1\1.xml";15 string xslt = @"C:\Users\user\Documents\Visual Studio 2015\Projects\ConsoleApp1\ConsoleApp1\1.xslt";16 XPathEngine engine = new XPathEngine();17 string result = engine.Execute(xmlPath, xpath, xslt);18 Console.WriteLine(result);19 Console.ReadLine();20 }21 }22}
Execute
Using AI Code Generation
1using NBi.Core.DataSerialization.Flattening.Xml;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7using System.Xml;8{9 {10 static void Main(string[] args)11 {12 XPathEngine xPathEngine = new XPathEngine();13 XmlDocument xmlDocument = new XmlDocument();
Execute
Using AI Code Generation
1using System;2using System.Xml;3using NBi.Core.DataSerialization.Flattening;4{5 {6 static void Main(string[] args)7 {8 var xml = new XmlDocument();9 xml.LoadXml("<root><node1><node2>value</node2></node1></root>");10 var engine = new XPathEngine();11 var result = engine.Execute(xml, "/root/node1/node2");12 Console.WriteLine(result);13 }14 }15}16using System;17using System.Xml;18using NBi.Core.DataSerialization.Flattening;19{20 {21 static void Main(string[] args)22 {23 var xml = new XmlDocument();24 xml.LoadXml("<root><node1><node2>value</node2></node1></root>");25 var engine = new XPathEngine();26 var result = engine.Execute(xml, "/root/node1/node2", "value");27 Console.WriteLine(result);28 }29 }30}31using System;32using System.Xml;33using NBi.Core.DataSerialization.Flattening;34{35 {36 static void Main(string[] args)37 {38 var xml = new XmlDocument();39 xml.LoadXml("<root><node1><node2>value</node2></node1></root>");40 var engine = new XPathEngine();41 var result = engine.Execute(xml, "/root/node1/node2", "value", "value");42 Console.WriteLine(result);43 }44 }45}46using System;47using System.Xml;48using NBi.Core.DataSerialization.Flattening;49{50 {51 static void Main(string[] args)52 {53 var xml = new XmlDocument();54 xml.LoadXml("<root><node1><node2>value</node2></node1></root>");55 var engine = new XPathEngine();56 var result = engine.Execute(xml, "/root/node1/node2", "value", "value", "value");
Execute
Using AI Code Generation
1using NBi.Core.DataSerialization.Flattening;2using System;3using System.Collections.Generic;4using System.Data;5using System.Linq;6using System.Text;7using System.Threading.Tasks;8using System.Xml;9{10 {11 static void Main(string[] args)12 {13 var xml = new XmlDocument();14 xml.LoadXml("<root><a><b>1</b><b>2</b></a><a><b>3</b><b>4</b></a></root>");15 var engine = new XPathEngine();16 var result = engine.Execute(xml, "/root/a/b");17 Console.WriteLine(result);18 Console.ReadLine();19 }20 }21}22using NBi.Core.DataSerialization.Flattening;23using System;24using System.Collections.Generic;25using System.Data;26using System.Linq;27using System.Text;28using System.Threading.Tasks;29using System.Xml;30{31 {32 static void Main(string[] args)33 {34 var xml = new XmlDocument();35 xml.LoadXml("<root><a><b>1</b><b>2</b></a><a><b>3</b><b>4</b></a></root>");36 var engine = new XPathEngine();37 var result = engine.Execute(xml, "/root/a/b");38 Console.WriteLine(result);39 Console.ReadLine();40 }41 }42}43XslCompiledTransform xslt = new XslCompiledTransform();44xslt.Load(xsltFilePath);45xslt.Transform(xmlFilePath, outputFilePath);46StringWriter sw = new StringWriter();47xslt.Transform(xmlFilePath, null, sw);48XmlWriterSettings settings = new XmlWriterSettings();49settings.Indent = true;50settings.IndentChars = "\t";51";
Execute
Using AI Code Generation
1using NBi.Core.DataSerialization.Flattening;2using System;3using System.Xml;4using System.Xml.XPath;5{6 {7 static void Main(string[] args)8 {9 var engine = new XPathEngine();10 var xml = new XmlDocument();11 xml.LoadXml("<root><child><grandchild>value</grandchild></child></root>");12 var result = engine.Execute(xml, "/root/child/grandchild");13 foreach (var node in result)14 {15 Console.WriteLine(node);16 }17 }18 }19}20using NBi.Core.DataSerialization.Flattening;21using System;22using System.Xml;23using System.Xml.XPath;24{25 {26 static void Main(string[] args)27 {28 var engine = new XPathEngine();29 var xml = new XmlDocument();30 xml.LoadXml("<root><child><grandchild>value</grandchild></child></root>");31 var result = engine.Execute(xml, "/root/child/grandchild");32 foreach (var node in result)33 {34 Console.WriteLine(node);35 }36 }37 }38}39using NBi.Core.DataSerialization.Flattening;40using System;41using System.Xml;42using System.Xml.XPath;43{44 {45 static void Main(string[] args)46 {47 var engine = new XPathEngine();48 var xml = new XmlDocument();49 xml.LoadXml("<root><child><grandchild>value</grandchild></child></root>");50 var result = engine.Execute(xml, "/root/child/grandchild");51 foreach (var node in result)52 {53 Console.WriteLine(node);54 }55 }56 }57}58using NBi.Core.DataSerialization.Flattening;59using System;60using System.Xml;61using System.Xml.XPath;62{63 {64 static void Main(string[] args)65 {66 var engine = new XPathEngine();67 var xml = new XmlDocument();68 xml.LoadXml("<root><child
Execute
Using AI Code Generation
1NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();2engine.Execute("1.xml", "2.xml", "3.xml", "4.xml");3NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();4engine.Execute("1.xml", "2.xml", "3.xml", "4.xml");5NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();6engine.Execute("1.xml", "2.xml", "3.xml", "4.xml");7NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();8engine.Execute("1.xml", "2.xml", "3.xml", "4.xml");9NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();10engine.Execute("1.xml", "2.xml", "3.xml", "4.xml");11NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();12engine.Execute("1.xml", "2.xml", "3.xml", "4.xml");13NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();14engine.Execute("1.xml", "2.xml", "3.xml", "4.xml");
Execute
Using AI Code Generation
1using NBi.Core.DataSerialization.Flattening;2using NBi.Core.DataSerialization.Flattening.Xml;3using System.Xml;4using System.Xml.XPath;5using System.IO;6using System;7{8 {9 static void Main(string[] args)10 {11 NBi.Core.DataSerialization.Flattening.Xml.XPathEngine xPathEngine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();12 XPathNavigator xPathNavigator = new XPathDocument("C:\\Users\\user\\Desktop\\1.xml").CreateNavigator();13 XPathExpression xPathExpression = xPathNavigator.Compile("/Root/Child");14 XPathNodeIterator xPathNodeIterator = xPathNavigator.Select(xPathExpression);15 XPathNodeIterator xPathNodeIterator1 = xPathNavigator.Select(xPathExpression);16 XPathNodeIterator xPathNodeIterator2 = xPathNavigator.Select(xPathExpression);17 XPathNodeIterator xPathNodeIterator3 = xPathNavigator.Select(xPathExpression);18 XPathNodeIterator xPathNodeIterator4 = xPathNavigator.Select(xPathExpression);19 XPathNodeIterator xPathNodeIterator5 = xPathNavigator.Select(xPathExpression);20 XPathNodeIterator xPathNodeIterator6 = xPathNavigator.Select(xPathExpression);21 XPathNodeIterator xPathNodeIterator7 = xPathNavigator.Select(xPathExpression);22 XPathNodeIterator xPathNodeIterator8 = xPathNavigator.Select(xPathExpression);23 XPathNodeIterator xPathNodeIterator9 = xPathNavigator.Select(xPathExpression);24 XPathNodeIterator xPathNodeIterator10 = xPathNavigator.Select(xPathExpression);25 XPathNodeIterator xPathNodeIterator11 = xPathNavigator.Select(xPathExpression);
Execute
Using AI Code Generation
1NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();2NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();3NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();4NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();5NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();6NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.XPathEngine();7NBi.Core.DataSerialization.Flattening.Xml.XPathEngine engine = new NBi.Core.DataSerialization.Flattening.Xml.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!!