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!
Related
<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");
}
}
}
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();
}
}
}
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());
}
}
}
I have xml fragment in an xml-File. The fragment has tags with namespace.
How can I read that xml fragment, which represent exactly an XElement element ?
<node id="n0::n0">
<data key="d6">
<y:ShapeNode>
<y:Geometry height="91.44" width="59.49119999999999" x="364.256180835285" y="-698.4404365079365"/>
<y:Fill color="#FFCC00" transparent="false"/>
<y:BorderStyle color="#000000" type="line" width="1.0"/>
<y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="internal" modelPosition="t" textColor="#000000" visible="true" width="48.677734375" x="5.406732812499968" y="4.0">MELEIN</y:NodeLabel>
<y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" textColor="#000000" visible="true" width="27.35546875" x="16.067865624999968" y="20.843814062500087">8,00<y:LabelModel>
<y:SmartNodeLabelModel distance="4.0"/>
</y:LabelModel>
<y:ModelParameter>
<y:SmartNodeLabelModelParameter labelRatioX="0.0" labelRatioY="-0.5" nodeRatioX="0.0" nodeRatioY="-0.2720492775317138" offsetX="0.0" offsetY="0.0" upX="0.0" upY="-1.0"/>
</y:ModelParameter>
</y:NodeLabel>
<y:Shape type="rectangle"/>
</y:ShapeNode>
</data>
</node>
I tried a lot of different ways the last one nearly comes to the target
var mngr = new System.Xml.XmlNamespaceManager(new System.Xml.NameTable());
mngr.AddNamespace(string.Empty, "urn: ignore"); // or proper URL
mngr.AddNamespace("y", "urn:ignore"); // or proper URL
var parserContext = new System.Xml.XmlParserContext(null, mngr, null, System.Xml.XmlSpace.None, null);
var txtReader = new System.Xml.XmlTextReader("block.graphml", System.Xml.XmlNodeType.Element, parserContext);
var ele = XElement.Load(txtReader);
but it crashes in the last line with System.InvalidOperationException
Is there any easy way to import xml fragment to an existing xelement?
I would prefer the way XElement.load("block.graphml"); This didn't work at all.
Thanks for your tips
You can do it like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.txt";
static void Main(string[] args)
{
string input = File.ReadAllText(FILENAME);
XElement element = XElement.Parse("<Root xmlns:y=\"www.mynamespace.com\"></Root>");
element.Add(input);
}
}
}
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.