Update a specific node in XML using XmlDocument - c#

I am new to managing XML, I've read a couple of articles but I'm confused when it comes to a specific XML that I am working on. Can someone help me with the right statement? I just want to update the value of the ListStart, but I am getting an error when compiling. I am updating that part by this:
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.Load(#"path");
XmlNode myNode = soapEnvelopeDocument.SelectSingleNode("descendant::cet:GetListCustomElement[cet:GetListCustom/cet:ListID='101']");
soapEnvelopeDocument.LastChild.InnerText = sDate;
<soapenv:Header/>
<soapenv:Body>
<cet:GetListCustomElement>
<!--Zero or more repetitions:-->
<cet:GetListCustom>
<cet:ListID>101</cet:ListID>
<cet:ListStart>13.11.2020</cet:ListStart>
</cet:GetListCustom>
</cet:GetListCustomElement>
</soapenv:Body>
</soapenv:Envelope>```

You only supplied a piece of the xml without the namespaces. With Xml linq you can get the element without the namespaces. See code below :
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 ListStart = doc.Descendants().Where(x => x.Name.LocalName == "ListStart").FirstOrDefault();
ListStart.SetValue("14.11.2020");
}
}
}

Related

How do I update an Old element with a new one in C#/XML

Basically what I want to do is: Download a new XML file and replace some elements with the old one, For example replace this code:
<Run x:Name="Degree" Text="15"/>
with current degree, which is
<temperature value="280.15" min="278.15" max="281.15" unit="kelvin"/>
but I don't know how to do that. Here's my code I'm stuck with:
using (WebClient web = new WebClient())
{
string url = string.Format("https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=b6907d289e10d714a6e88b30761fae22");
var xml = web.DownloadString(url);
}
Use 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 URL = "https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=b6907d289e10d714a6e88b30761fae22";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(URL);
XElement temperature = doc.Descendants("temperature").FirstOrDefault();
temperature.SetAttributeValue("value", 281);
string oldXml = "<Root xmlns:x=\"abc\"><Run x:Name=\"Degree\" Text=\"15\"/></Root>";
XDocument oldDoc = XDocument.Parse(oldXml);
XElement run = oldDoc.Descendants("Run").FirstOrDefault();
run.ReplaceWith(temperature);
}
}
}

I need to read xml file data but due to attribute it does not work

i want to read xml file but due to Document node attribute it did not read file.
Code C#:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(HttpContext.Server.MapPath("~/Content/Images/MMS-CREATE-ALLA-ALLAH2H1-23102018-000170-INP.xml"));
XmlNode settings = xmldoc.SelectSingleNode("Document[#xmlns='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01']/MndtInitnReq/GrpHdr");
stu.BranchName = settings.SelectSingleNode("MsgId").InnerText;
XML FIle:
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.009.001.01">
<MndtInitnReq>
<GrpHdr>
<MsgId>10005226074</MsgId>
<CreDtTm>2018-10-23T15:20:56</CreDtTm>
</GrpHdr>
</MndtInitnReq>
</Document>
You have a namespace that must be used to get the data. Try Xml Linq :
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication75
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XNamespace ns = doc.Root.GetDefaultNamespace();
string msgId = (string)doc.Descendants(ns + "MsgId").FirstOrDefault();
XElement xCreDtTm = doc.Descendants(ns + "CreDtTm").FirstOrDefault();
//will give 1/1/01 when null
DateTime CreDtTm = xCreDtTm == null ? new DateTime() : (DateTime)xCreDtTm;
}
}
}
I don't think loading this xml should be a problem. I verified that by loading the xml you posted in an XmlDocument object.
However I think, your xpath to get "settings" node should have xml namespace in all tags after Document.
So the xpath should be "/[local-name()='Document' and namespace-uri()='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01']/[local-name()='MndtInitnReq' and namespace-uri()='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01']/*[local-name()='GrpHdr' and namespace-uri()='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01']"

XmlDocument.SelectNodes and XPath Navigation

I have XML code that looks similar to this:
<BookStore xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Book>
<bookGenre>Fantasy</bookGenre>
<bookTitle>A Storm of Swords</bookTitle>
<authorInformation>
<authorId>12345</authorId>
<authorName>
<firstName>George</firstName>
<middleInitial>R.R.</middleInitial>
<lastName>Martin</lastName>
</authorName>
</authorInformation>
</Book>
<customer>
<customerData />
</customer>
</BookStore>
The <customer>node may or may not have child nodes, depending on user input.
I am trying to use XmlDocument.SelectNodes and XPath navigation to select <BookStore>, <customer>, and any nodes contained within <customer>.
I have been looking around and reading about XPath and .SelectNodes for a few hours but still don't seem to fully understand how they work. Would anyone have an explanation as to how to use them or how I could use them in my situation? If there are other ways to go about solving my problem I'm open to those too! (I am using C#)
EDIT: here's what I tried based on the stuff I read
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlStr);
XmlNode root = doc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
XmlNodeList nodeList = root.SelectNodes("descendant::customer:child::Node");
doc.Save(Console.Out);
Try xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication62
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement customer = doc.Descendants("customer").FirstOrDefault();
Boolean children = customer.HasElements;
}
}
}

Fast replacement node names in XML

I use a legacy service that manage Fetch CRM: crmService.Fetch(fetchXml).
I get XML string result like this:
<resultset>
<result>
<new_categoria name="Cliente" formattedvalue="1">1</new_categoria>
<new_name>Admin</new_name>
<new_tipodecampanaid>{F8F29978-4E0F-AE92-FB43-48B4DC406B1F}</new_tipodecampanaid>
<statuscode name="Activo">0</statuscode>
</result>
<result>
<new_categoria name="Client" formattedvalue="1">1</new_categoria>
<new_name>Client</new_name>
<new_tipodecampanaid>{758341BA-4661-D694-6743-8D2DC875793E}</new_tipodecampanaid>
<statuscode name="Activo">0</statuscode>
</result>
<result>
</resultset>
We need (because alias not support for Fetch method) replace several node names:
1 - Replace new_categoria node name by org_category
2 - Replace new_name node name by org_name
3 - Replace new_tipodecampanaid node name by org_campaignid
We need high peformance, maybe results can be huge.
Using XmlDocument:
var doc = new XmlDocument();
doc.LoadXml(fetchXMLResult);
XmlNode root = doc.SelectSingleNode("resultset");
foreach (XmlNode childNode in root.ChildNodes)
{
}
Using XDocument:
XDocument resultset = XDocument.Parse(fetchXMLResult);
if (resultset.Root == null || !resultset.Root.Elements("result").Any())
{
return;
}
resultset.Root.Elements("result")
Any suggestions?
For speed you could run through the file using an XmlReader and write each node you read to a new file using an XmlWriter.
See this link for an example.
When you have large xml file always use XmlReader. Try this code
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 INPUT_FILENAME = #"c:\temp\test1.xml";
const string OUTPUT_FILENAME = #"c:\temp\test2.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(INPUT_FILENAME);
XmlWriter writer = XmlWriter.Create(OUTPUT_FILENAME);
writer.WriteStartElement("resultset");
while (!reader.EOF)
{
if (reader.Name != "result")
{
reader.ReadToFollowing("result");
}
if (!reader.EOF)
{
XElement result = (XElement)XElement.ReadFrom(reader);
result.Element("new_categoria").Name = "org_category";
result.Element("new_name").Name = "org_name";
result.Element("new_tipodecampanaid").Name = "org_campaignid";
writer.WriteRaw(result.ToString());
}
}
writer.WriteEndElement();
writer.Flush();
writer.Close();
}
}
}

Reading the XMl Response and Extracting the nodes using c#

I am trying to extract the XML Response from an API. My XML response looks like below. I tried different methods. can you please help me in reading and extracting the individual nodes from the XMl response.
For Example: I want to extract node from below XML response.
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<RichDBDS><TrxDetailCard><TRX_HD_Key>5437845</TRX_HD_Key> <Invoice_ID>189809</Invoice_ID><Date_DT>3/24/2016 1:34:44 PM</Date_DT><Merchant_Key>2202</Merchant_Key><Reseller_Key>2</Reseller_Key><TUser_Name_VC>ITSTEST.ISMI</TUser_Name_VC><Processor_ID>CC Processor</Processor_ID><TRX_Settle_Key></TRX_Settle_Key><Tip_Amt_MN></Tip_Amt_MN><Approval_Code_CH>24032016013444</Approval_Code_CH><Auth_Amt_MN>184.99</Auth_Amt_MN><IP_VC>66.55.53.68</IP_VC><Account_Type_CH>MANUAL</Account_Type_CH><Last_Update_DT></Last_Update_DT><Orig_TRX_HD_Key></Orig_TRX_HD_Key><Settle_Date_DT></Settle_Date_DT><Settle_Flag_CH>FALSE</Settle_Flag_CH><Trans_Type_ID>Sale</Trans_Type_ID><Void_Flag_CH>FALSE</Void_Flag_CH><CustomerID></CustomerID><AVS_Resp_CH>Y</AVS_Resp_CH><CV_Resp_CH>M</CV_Resp_CH><Host_Ref_Num_CH>0b144718-3597-4cfb-a2b5-058679510f29</Host_Ref_Num_CH><Zip_CH>30328</Zip_CH><Acct_Num_CH>1111</Acct_Num_CH><Total_Amt_MN>184.99</Total_Amt_MN><Exp_CH>0917</Exp_CH><Name_on_Card_VC>Kenneth Boler</Name_on_Card_VC><Type_CH>VISA</Type_CH><Cash_Back_Amt_MN></Cash_Back_Amt_MN><Result_CH>0</Result_CH><Result_Txt_VC>approval</Result_Txt_VC><Trans_Status>Settled</Trans_Status><PO_Num></PO_Num></TrxDetailCard></RichDBDS>
Try code like below using XML Linq. I used Load(FILENAME or URL) method, but you can also use Parse(STRING).
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);
var results = doc.Descendants().Where(x => x.Name.LocalName == "TrxDetailCard").Select(y => new {
TRX_HD_Key = (int)y.Element(y.Name.Namespace + "TRX_HD_Key"),
Invoice_ID = (int)y.Element(y.Name.Namespace + "Invoice_ID"),
Date_DT = (DateTime)y.Element(y.Name.Namespace + "Date_DT"),
Merchant_Key = (int)y.Element(y.Name.Namespace + "Merchant_Key")
}).ToList();
}
}
}

Categories

Resources