C# XmlDocument for grabbing API specific data? - c#

I am trying to grab the specific value of an attribute in:
http://data.alexa.com/data?cli=10&dat=snbamz&url=bing.com
<SD>
<POPULARITY URL="bing.com/" TEXT="16" SOURCE="panel"/>
<REACH RANK="16"/>
<RANK DELTA="-7"/>
<COUNTRY CODE="US" NAME="United States" RANK="9"/>
</SD>
</ALEXA>
I want to grab the value of
I have the current Console Code for this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string url = "http://data.alexa.com/data?cli=10&dat=snbamz&url=bing.com";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(url);
XmlNode root = xmldoc.SelectSingleNode("//#RANK");
//XmlNamespaceManager xnm1 = new XmlNamespaceManager(xmldoc.NameTable);
//XmlNodeList nList1 = xmldoc.SelectNodes("//#RANK", xnm1);
Console.WriteLine(root.ToString());
Console.ReadLine();
}
}
}
But when I run it, I receive the following message in return:
System.Xml.XmlAttribute
What am I doing wrong?

Try changing:
Console.WriteLine(root.ToString());
to:
Console.WriteLine(root.Value);
Hope that helps.

Related

How to replace Xml Attribute Prefix value by using C#

<ce_table frame="topbot" id="t0010" rowsep="0" colsep="0">
<ce_label>Table 2</ce_label>
<ce_caption id="cn040">
<ce_simple-para id="spar055">Model fit cnbs for the span targeted moments.</ce_simple-para>
</ce_caption>
</ce_label>
</ce_table>
I need to change id="t0010" to id="tf0010" and id="cn.. " to id="cib.. ".
I only need to change prefix of an attribute value.
Use xml linq :
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication2
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement ce_table = doc.Descendants("ce_table").First();
ce_table.SetAttributeValue("id", "tf0010");
XElement ce_caption = ce_table.Descendants("ce_caption").First();
ce_caption.SetAttributeValue("id", "cib040");
}
}
}

Update multiple XML nodes that match an Xpath query C#

I am trying to update the content of multiple nodes that match a specific xpath. For example, in the below example, I want to update any attribute1 node that currently is '2' with 'Hello'. What is the best way to do this?
(DotNetFiddle Here)
using System;
using System.IO;
using System.Xml;
using System.Linq;
public class Program
{
public static void Main()
{
XmlDocument job = new XmlDocument();
job.LoadXml(#"<parent>" +
"<report>" +
"<main>" +
"<attribute1>2</attribute1>"+
"<attribute1>2</attribute1>"+
"<attribute1>3</attribute1>"+
"</main>"+
"</report>" +
"</parent>");
string newAttribute1Value = "Hello";
//How do I update both attribute1's where value=2?
}
This may could help you :
using System;
using System.IO;
using System.Xml;
using System.Linq;
public class Program
{
public static void Main()
{
XmlDocument job = new XmlDocument();
job.LoadXml(#"<parent>" +
"<report>" +
"<main>" +
"<attribute1>2</attribute1>"+
"<attribute1>2</attribute1>"+
"<attribute1>3</attribute1>"+
"</main>"+
"</report>" +
"</parent>");
string newAttribute1Value = "Hello";
//How do I update both attribute1's where value=2?
// getting the list of nodes with XPath query :
XmlNodeList nodes = job.SelectNodes("//attribute1[text()=2]");
foreach (XmlNode child in nodes)
{
// replacing the old value with the new value
child.InnerText = newAttribute1Value ;
}
}

Modify xml file with C#

I have an XML file content as below
and I need to modify line 4:
701.50,24.0 to 701.50,30.0
How can I do this?
<CableLossConfig>
<Std Val="CATM1">
<Path Val="TX1">
<Loss>701.50,24.0</Loss>
<Loss>710.50,24.0</Loss>
<Loss>713.50,24.0</Loss>
<Loss>779.50,23.0</Loss>
<Loss>782.00,23.0</Loss>
<Loss>784.50,23.0</Loss>
<Loss>826.50,30.0</Loss>
<Loss>836.50,30.0</Loss>
<Loss>846.50,30.0</Loss>
<Loss>1712.50,37.0</Loss>
<Loss>1732.50,37.0</Loss>
<Loss>1752.50,37.0</Loss>
<Loss>1852.50,37.0</Loss>
<Loss>1880.00,37.0</Loss>
<Loss>1907.50,37.0</Loss>
</Path>
</Std>
</CableLossConfig>
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement tx1 = doc.Descendants("Path").Where(x => (string)x.Attribute("Val") == "TX1").FirstOrDefault();
List<XElement> losses = tx1.Elements("Loss").ToList();
XElement loss1 = losses[1];
loss1.SetValue("701.50,30.0");
}
}
}
If you would like a simple solution that does not require creating a C# model representing the structure of the xml file, I would suggest using the XDocument .net class (https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument?view=netframework-4.8)
I found a quite nice example here on stackoverflow as well where a similar question was asked: Edit specific Element in XDocument
I hope this helps!

UFT API Test How can I extract and save XLS values from the XML response?

I am new to the UFT API Test, and I need to extract certain values ​​from the XML response to be included in an existing XLS, which will serve as input data for another test.
This is my XML Response:
<NS1:Body>
<NS3:BuscaSaldosCaptacionOut xmlns:NS3="http://www.portal.com/ws/esb/ConsultaCuentasSaldos">
<SaldosCaptacion>
<NumeroCliente>51844068</NumeroCliente>
<Cuenta>0201484326</Cuenta>
<ProductoCuenta>01</ProductoCuenta>
<SubProductoCuenta>0382</SubProductoCuenta>
<Divisa>MXP</Divisa>
<IdCuenta>0201484326</IdCuenta>
<SaldoInicialDia>7062.42</SaldoInicialDia>
<SaldoPromedio>30596.01</SaldoPromedio>
<SaldoActual>17062.42</SaldoActual>
<SaldoDisponible>17062.42</SaldoDisponible>
<EstatusCuenta>A</EstatusCuenta>
</SaldosCaptacion>
<SaldosCaptacion>
<NumeroCliente>51844068</NumeroCliente>
<Cuenta>0201484371</Cuenta>
<ProductoCuenta>01</ProductoCuenta>
<SubProductoCuenta>0340</SubProductoCuenta>
<Divisa>MXP</Divisa>
<SaldoInicialDia>6825.11</SaldoInicialDia>
<SaldoPromedio>8936.26</SaldoPromedio>
<SaldoActual>6825.11</SaldoActual>
<SaldoDisponible>6825.11</SaldoDisponible>
<SaldoRetenido>0.00</SaldoRetenido>
<EstatusCuenta>A</EstatusCuenta>
</SaldosCaptacion>
<SaldosCaptacion>
<NumeroCliente>51844068</NumeroCliente>
<Cuenta>0201533729</Cuenta>
<ProductoCuenta>01</ProductoCuenta>
<SubProductoCuenta>0363</SubProductoCuenta>
<Divisa>MXP</Divisa>
<SaldoInicialDia>28316.52</SaldoInicialDia>
<SaldoPromedio>6230.52</SaldoPromedio>
<SaldoActual>7374.52</SaldoActual>
<SaldoDisponible>7374.52</SaldoDisponible>
<SaldoRetenido>942.00</SaldoRetenido>
<EstatusCuenta>A</EstatusCuenta>
</SaldosCaptacion>
</NS3:BuscaSaldosCaptacionOut>
I need save "Cuenta" and "SaldoActual" values. (Everybody), and completely ignore how to parameterize them.
The "Write to file" option does not work for me.
Try following code. If it doesn't work then the namespace needs to be changed in code. I can't tell the correct namespace because you did post the beginning of the xml file. I'm using xml linq which is a Net Library.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XElement doc = XElement.Parse(xml);
var results = doc.Descendants("SaldosCaptacion").Select(x => new {
Cuenta = (string)x.Element("Cuenta"),
SaldoActual = (decimal)x.Element("SaldoActual")
}).ToList();
}
}
}

How to fetch specific data from XML using C#

I'm trying to fetch specific data from a Xml file to display in a textbox. My Xml file name as "test.xml" has following code
<?xml version="1.0" encoding="utf-8" ?>
<Body>
<Context>
<PageNo>a87</PageNo>
<Verse>"Do it right"</Verse>
</Context>
</Body>
My C# code is below: Edited to reflect recent chnages
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace learn2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pageid1();
}
private void pageid1()
{
textBox1.Clear();
XmlDocument doc = new XmlDocument();
doc.Load("C:\\test.xml");
var pagenoNodeList = doc.SelectNodes("Body/Context/PageNo");
var pageNoNode = pagenoNodeList[0]; // To select the first node
var text = pageNoNode.InnerText; // Gets the text value inside the node
textBox1.Text = text;
} }
}
I highly appreciate any help. Thanks
You need to identify the node you are looking for, as SelectNodes returns a System.Xml.XmlNodeList, and then get the value of InnerText:
var pagenoNodeList = doc.SelectNodes("Body/Context/PageNo");
var pageNoNode = pagenoNodeList[0]; // To select the first node
var text = pageNoNode.InnerText; // Gets the text value inside the node
textBox1.Text = text;

Categories