Is there a specific size limit for WebDriver IWebElements? - c#

I have a C# application that opens a website and reads a raw XML dump that gets generated inside of a "textarea" CSS element. I am using the Selenium Firefox web driver.
IWebElement body = driver.FindElement(By.CssSelector("textarea"));
string xmlData = body.Text;
No errors are thrown and the string xmlData contains part of the data but not all of it - 902 lines of text compared to 32,255 lines if I copy and paste it directly from the website. What surprises me is that xmlData seems to be missing a large chunk of data out of the middle, not the beginning or end.
Is there a limit to the size of IWebElement and what are my alternatives to capturing this XML dump?
EDIT: A bit more info.
Here is the output of xmlData where the block of data is missing. Notice the ellipses.
<row>
<value>ATL</value>
<value>Overnight</value>
...s:nil="true" />
<value xs:nil="true" />
<value xs:nil="true" />
</row>
And here is the data copied directly from the XML dump on the website showing the full data:
<row>
<value>ATL</value>
<value>Overnight</value>
<value>737</value>
<value>3BX</value>
<value>SIC</value>
</row>
*Insert thousands of rows here*
<row>
<value>TPA</value>
<value>Turnaround</value>
<value xs:nil="true" />
<value xs:nil="true" />
<value xs:nil="true" />
</row>

Try to get the HTMLTextAreaElement.value property instead:
IWebElement body = driver.FindElement(By.CssSelector("textarea"));
string xmlData = body.GetAttribute("value");

The limitation turned out to be Visual Studio's visualizing tool (text visualizer) and not a limitation with IWEbElements.

Related

How to insert an XML File into Another XML File at Specific Node C#

I have two XML Files ,the first one is and Named as XMLTemplate
<DataSources>
<DataSource Name="XXXX">
</DataSource>
<DataSource Name="ABC">
</DataSource>
</DataSources>
<DataSets>
<DataSet Name="abc">
<Query>
</Query>
<ReportSections>
</ReportSections>
and the second xml file is named as XMLGenrated,
<Fields>
<Field >
</Field>
</Fields>
and I need the Output as,
<DataSource Name="XXXX">
</DataSource>
<DataSource Name="ABC">
</DataSource>
</DataSources>
<DataSets>
<DataSet Name="abc">
<Query>
<Fields>
<Field >
</Field>
</Fields>
</Query>
<ReportSections>
</ReportSections>
Both the Files are in .XML Extension and I dont know how to find the node by its Name Can anyone help me out.
I tried this,
XElement xFileRoot = XElement.Load(XMLTemplate.xml);
XElement xFileChild = XElement.Load(XMLGenerated.xml);
xFileRoot.Add(xFileChild);
xFileRoot.Save(file1.xml);
but the XML adds below the XMLTemplate I dont know how to insert at particular node.
Find the node using Linq to XML and Replace its contents
XElement xFileRoot = XElement.Load(XMLTemplate.xml);
XElement xFileChild = XElement.Load(XMLGenerated.xml);
var queryNode = xFileRoot.Element("Query");
queryNode.ReplaceWith(xFileChild) ;
Based on on this answer - How can I update/replace an element of an XElement from a string?
Be aware that you sample XML files contain multiple root nodes, and that if you need to keep the <Query> node you need to change this.

XML Traffic Copping

I have an "xml-source" that is sending me XML.
The xml can come in two ways. One that is "Order" related. A second that is "Employee" related.
Typically, when I shred xml, I use some Linq-To-Xml to parse a known xml-schema.
But now, with 2 different xml-schema's coming at me, I am trying to find the most efficient way to "traffic cop" the Xml.
What I've tried is:
Parse "version1".........and then check for mandatory (order) data (like valid OrderId's). If that data isn't there, then:
Parse for "version2"........then check for mandatory employee data, like employeeSSN.
If both fail, then I log that I didn't get either.
Any ideas on what is the most efficient way to do this traffic copping of the Xml?
My other idea is the create xsd's, and validate. But then I'm running a xsd-validate....and then sending it to a "parser".....so that may be less efficient than the idea above...of try1, try2, then fail.
While I can choose when I traffic-cop the xml, I cannot change the way the xml is sent to me. The xml is put on a queue, and the architecture does not allow for putting different xml's on different queues.....via contract with the third party vendor.
Any idea that I'm not seeing?
I don't like different xml's on the same queue............but that's ship has sailed.
The xml's below are made up........but show that the xml's are just completely different and share nothing.
<root>
<orders>
<order orderId="ABC123">
<orderDetails>
<orderDetail itemName="Bike" quantity="1"/>
<orderDetail itemName="TeddyBear" quantity="2"/>
<orderDetail itemName="Doll" quantity="3"/>
</orderDetails>
</order>
<!-- -->
<order orderId="DEF234">
<orderDetails>
<orderDetail itemName="Truck" quantity="4"/>
<orderDetail itemName="Marbles" quantity="5"/>
<orderDetail itemName="BoardGame" quantity="6"/>
</orderDetails>
</order>
</orders>
</root>
<root>
<employees>
<employee employeeSSN="222222222" firstName="John">
<employeeEmails>
<employeeEmail emailValue="john#home.com" />
<employeeEmail emailValue="john#work.com" />
</employeeEmails>
</employee>
<!-- -->
<employee employeeSSN="3333333" firstName="Mary">
<employeeEmails>
<employeeEmail emailValue="mary#home.com" />
<employeeEmail emailValue="mary#work.com" />
<employeeEmail emailValue="mary#spamcatcher.com" />
</employeeEmails>
</employee>
</employees>
</root>

How to use LINQ to get data from an XML file?

I have xml files which look like this:
<?xml version="1.0" encoding="utf-8"?>
<record id="177" restricted="false">
<type>record type</type>
<startdate>2000-10-10</startdate>
<enddate>2014-02-01</enddate>
<titles>
<title xml:lang="en" type="main">Main title</title>
<!-- only one title element with type main -->
<title xml:lang="de" type="official">German title</title>
<!-- can have more titles of type official -->
</titles>
<description>description of the record</description>
<categories>
<category id="122">
<name>category name</name>
<description>category description</description>
</category>
<!-- can have more categories -->
</categories>
<tags>
<tag id="5434">
<name>tag name</name>
<description>tag description</description>
</tag>
<!-- can have more tags -->
</tags>
</record>
How do I select the data from these xml files using LINQ, or should I use something else?
You can load xml into XDocument objects using either the Load() method
for files, or the Parse() method for strings:
var doc = XDocument.Load("your-file.xml");
// OR
var doc = XDocument.Parse(yourXmlString);
Then you can access the data using LINQ:
var titles =
from title in doc.XPathSelectElements("//title")
where title.Attribute("type").Value == "official"
select title.Value;
Was searching for examples of Xmlserializer and found this: How to Deserialize XML document
So why not to try. I did Ctrl+C and Edit -> Paste Special -> Paste XML As Classes in Visual Studio 2013 and... Whoa I got all the classes generated. One condition target framework must be 4.5 and this function is available from Visual Studio 2012+ (as stated in that post)

Extracting App version number from XML with C#

I have an XML file as follows. I would like to extract the Version number from this file.
I tried XML parsing. But, that is for node values only. I am able to get this file as string as follows. var doc = XDocument.Load("WMAppManifest.xml");
<?xml version="1.0" encoding="utf-8"?>
<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2012/deployment" AppPlatformVersion="8.0">
<DefaultLanguage xmlns="" code="en-US" />
<App xmlns="" ProductID="{a3f55b1e-c183-4645-9b19-87a41a206978}" Title="sometitle" RuntimeType="Silverlight" Version="1.0.0.0" Genre="apps.normal" Author="M-Files author" BitsPerPixel="32" Description="Apache Cordova for Windows Phone" Publisher="CordovaExample" PublisherID="{b93a0d8e-5aa9-4d9b-b232-17e2d852e779}">
<IconPath IsRelative="true" IsResource="false">ApplicationIcon.png</IconPath>
</App>
</Deployment>
You can access the XML declaration node as follows:
XmlDeclaration declaration = doc.ChildNodes
.OfType<XmlDeclaration>()
.FirstOrDefault();
You can then read the value of declaration.Version.
Or, if you are after the 'app' version attribute in the XML document itself, try the following
string version = doc.Descendants("App")
.Single()
.Attribute("Version").Value

XML to LINQ with metadata

I'm very new to both XML and LINQ. I've read several XML to LINQ tutorials, but none of the XML documents seem to be formatted the way mine is. I'm not sure if (and how) it changes things.
All the examples I've read on the internet seem to follow this format:
<data>
<row>
<Term>201320</Term>
<Subj>ACCT</Subj>
<Subj_desc>Accounting</Subj_desc>
</row>
<row>
<Term>201320</Term>
<Subj>ACCT</Subj>
<Subj_desc>Accounting</Subj_desc>
</row>
</data>
If I wanted to read that I think the code would look something like this:
XDocument document = XDocument.Load("URLHERE.xml");
var term = from row in document.Descendants("row")
select new
{
Term = row.Element("Term").Value,
Subject = row.Element("Subj").Value,
Subject_Description = row.Element("Subj_desc").Value,
};
Here's the problem: my XML document doesn't follow the same format. Instead of repeating the different tags for each term, it has a set of metadata at the top and then uses the SAME tag for each set of data.
Here's a sample of my XML document:
<metadata>
<item name="TERM" type="xs:string" length="128"/>
<item name="SUBJ" type="xs:string" length="128"/>
<item name="SUBJECT_DESC" type="xs:string" length="512"/>
</metadata>
<data>
<row>
<value>201320</value>
<value>ACCT</value>
<value>Accounting</value>
</row>
<row>
<value>201320</value>
<value>ACCT</value>
<value>Accounting</value>
</row>
</data>
How would I extract data from it?
var term = from row in document.Descendants("row")
select new
{
Term = row.Element("value").Value,
Subject = row.Element("value").Value,
};
Doesn't seem right. I'm using C# BTW (not sure if I need to say that or if the tag's sufficient).
Your XML is not properly formatted, you need a root element that encapsulates the entire document. such as
<?xml version='1.0' encoding='utf-8'?>
<root>
<metadata>
<item name="TERM" type="xs:string" length="128"/>
<item name="SUBJ" type="xs:string" length="128"/>
<item name="SUBJECT_DESC" type="xs:string" length="512"/>
</metadata>
<data>
<row>
<value>201320</value>
<value>ACCT</value>
<value>Accounting</value>
</row>
<row>
<value>201320</value>
<value>ACCT</value>
<value>Accounting</value>
</row>
</data>
</root>
Then using XDocument you could load the file
var doc = XDocument.Load("file.xml");
then you can extract the data, kinda depends what you want to extract, you never specified. example of getting the metadata
var item = doc.Descendants("metadata");
getting rows, containing an IEnumerable of values
XDocument document = XDocument.Load("c:\\tmp\\test.xml");
var rows = from i in document.Descendants("row")
select new {values = i.Elements("value").Select(a=>a.Value)};

Categories

Resources