XML and JSP parsing in C# - c#

I have this XML feed file and I don`t know how to handle a problem.
So, the code is:
<PRICE>
<WIC>GA-H110M-S2H</WIC>
<DESCRIPTION>
GIGABYTE Main Board Desktop INTEL H110 (Socket LGA1151,2xDDR4,VGA/HDMI/DVI,1xPCIEX16/2xPCIEX1,USB3.0/USB2.0, 6xSATA III,LAN) micro ATX retail
</DESCRIPTION>
<VENDOR_NAME>GIGABYTE</VENDOR_NAME>
<GROUP_NAME>Main Board Desktop</GROUP_NAME>
<VPF_NAME/>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<AVAIL>0</AVAIL>
<RETAIL_PRICE>56.40</RETAIL_PRICE>
<MY_PRICE>52.71</MY_PRICE>
<WARRANTYTERM>36</WARRANTYTERM>
<GROUP_ID>32</GROUP_ID>
<VENDOR_ID>170192</VENDOR_ID>
<SMALL_IMAGE>
https://www.it4profit.com/catalogimg/wic/1/GA-H110M-S2H
</SMALL_IMAGE>
<PRODUCT_CARD>
https://content.it4profit.com/itshop/itemcard_cs.jsp?ITEM=151118121920215716&THEME=asbis&LANG=ro
</PRODUCT_CARD>
<EAN>4719331837310</EAN>
</PRICE>
Now, in PRODUCT_CARD are technical information for that product and I don`t know how to extract all data and then export to .csv using C# for example.

I haven't tried , but I am suggesting can do , this
Credits: Pulling data from a webpage, parsing it for specific pieces, and displaying it
string Url = "https://content.it4profit.com/itshop/itemcard_cs.jsp?ITEM=151118121920215716&THEME=asbis&LANG=ro";
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(Url);

Real simple to get into dictionary
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);
Dictionary<string, string> dict = doc.Descendants("PRICE").Elements()
.GroupBy(x => x.Name.LocalName, y => (string)y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}

Related

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();
}
}
}

Create Protege Readable Ontology from .NET

I am trying to create a protégé readable ontology from .NET.
I started with a 4GB .nt file and parsed out the needed classes and instances that I wish to use. Those are stored in memory since I got it down to less than 1 minute and roughly 1GB. They are in the form of Dictionary<String,HashSet<String>> right now. The next step is to take that data and move it into an OWL Ontology. Is there anywhere to start with how to manually loop through and do this? All of my research points me to using Manchester OWL, but everything I can find is for use with existing tools that don't fit my needs. I am looking for just doing a simple loop probably with LINQ to XML and I am unsure of how to format that or where to look to find how to do this.
Thanks
Jimmy
You can start with this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string definition =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Ontology xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xsi:schemaLocation=\"http://www.w3.org/2002/07/owl# http://www.w3.org/2009/09/owl2-xml.xsd\"" +
" xmlns=\"http://www.w3.org/2002/07/owl#\"" +
" xml:base=\"http://example.com/myOntology\"" +
" ontologyIRI=\"http://example.com/myOntology\">" +
"</Ontology>";
XDocument doc = XDocument.Parse(definition);
XElement ontology = (XElement)doc.FirstNode;
XNamespace ns = ontology.Name.Namespace;
ontology.Add(new XElement[] {
new XElement(ns + "Prefix", new XAttribute[] {
new XAttribute("name", "myOnt"),
new XAttribute("IRI", "http://example.com/myOntology#")
}),
new XElement(ns + "Import", "http://example.com/someOtherOntology")
});
}
}
}
​

Unable to Parse XMl after unwrapping soap body

I have been trying hard to parse an xml string, but all to no avail
<EnquirySingleItemResponse xmlns="http://tempuri.org/"> <EnquirySingleItemResult>
<Response>
<MSG>SUCCESS</MSG>
<INFO>TESTING</INFO>
</Response> </EnquirySingleItemResult> </EnquirySingleItemResponse>
My Code returns null or the texts in the xml tags no matter how i parse it. I have checked some post, but they seem not to be working.
See my code snipped below
XElement anotherUnwrappedResponse = ( from _xml in axdoc.Descendants(tempuri + "EnquirySingleItemResponse")
select _xml).FirstOrDefault();
string response = anotherUnwrappedResponse.Value;
axdoc.Descendants is used because i unwrapped the soap body, to have the xml above
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input =
"<EnquirySingleItemResponse xmlns=\"http://tempuri.org/\">" +
"<EnquirySingleItemResult>" +
"<Response>" +
"<MSG>SUCCESS</MSG>" +
"<INFO>TESTING</INFO>" +
"</Response> </EnquirySingleItemResult> </EnquirySingleItemResponse>";
XDocument doc = XDocument.Parse(input);
string msg = doc.Descendants().Where(x => x.Name.LocalName == "MSG").FirstOrDefault().Value;
string info = doc.Descendants().Where(x => x.Name.LocalName == "INFO").FirstOrDefault().Value;
}
}
}
​

C# XmlDocument for grabbing API specific data?

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.

Categories