Remove XML node in C# - c#

My XML file looks like this:
<root>
<files>
<filesource uploadName="20180131T083441_638_Test.doc" originalName="Test.doc">c:\uploadDir\20180131T083441_638_Test.doc</filesource>
<filesource uploadName="20180131T083441_638_Test1.doc" originalName="Test1.doc">c:\uploadDir\20180131T083441_638_Test1.doc</filesource>
<filesource uploadName="20180131T083441_638_Test2.doc" originalName="Test2.doc">c:\uploadDir\20180131T083441_638_Test2.doc</filesource>
</files>
</root>
C# code:
// fileNodes has all 3 files sources
XmlNode filesNodes = xmlFile.findNode("/root/files");
List<string> filenames = new List<string>();
filesname.add("c:\uploadDir\20180131T083441_638_Test.doc");
filesname.add("c:\uploadDir\20180131T083441_638_Test1.doc");
Whatever variable filenames contains, I want to keep it in XML file and the rest I want to delete.

Used xml linq. Had to remove the folder name from the file name to get code to work
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication23
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
List<string> filenames = new List<string>();
filenames.Add(#"c:\uploadDir\20180131T083441_638_Test.doc");
filenames.Add(#"c:\uploadDir\20180131T083441_638_Test1.doc");
List<string> baseFilenames = filenames.Select(x => x.Substring(x.LastIndexOf("\\") + 1)).ToList();
XDocument doc = XDocument.Load(FILENAME);
List<XElement> filesources = doc.Descendants("filesource").ToList();
foreach (XElement filesource in filesources)
{
string uploadName = (string)filesource.Attribute("uploadName");
if (!baseFilenames.Contains(uploadName))
filesource.Remove();
}
}
}
}

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

Creating dynamic xml file

I want to create 1095 e-file xml using c# code. For that i want to create dynamic tags that means if data is available then only creating tags otherwise it can not show that tag.
Here is sample of using xml linq to create your xml file. You can add IF statements as necessary to skip adding any elements
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 header = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><root xmlns:irs=\"myUrl\"></root>";
XDocument doc = XDocument.Parse(header);
XElement root = doc.Root;
XNamespace irs = root.GetNamespaceOfPrefix("irs");
XElement businessName = new XElement("BusinessNameLine1Txt", "Comp Name");
root.Add(businessName);
XElement tinRequest = new XElement(irs + "TINRequestTypeCd");
root.Add(tinRequest);
XElement employerEIN = new XElement(irs + "EmployerEIN", "899090900");
root.Add(employerEIN);
XElement contactNameGrp = new XElement("ContactNameGrp");
root.Add(contactNameGrp);
XElement firstName = new XElement("PersonFirstNm", "First Name Person");
contactNameGrp.Add(firstName);
XElement lastName = new XElement("PersonLastNm", "Last Name Person");
contactNameGrp.Add(lastName);
}
}
}
//sample output
//<?xml version="1.0" encoding="utf-8" ?>
//<root xmlns:irs="myUrl">
//<BusinessName>
// <BusinessNameLine1Txt>Comp Name</BusinessNameLine1Txt>
//</BusinessName>
//<irs:TINRequestTypeCd>BUSINESS_TIN</irs:TINRequestTypeCd>
//<irs:EmployerEIN>899090900</irs:EmployerEIN>
//<ContactNameGrp>
// <PersonFirstNm>First Name Person</PersonFirstNm>
// <PersonLastNm>Last Name Person</PersonLastNm>
//</ContactNameGrp> <ContactPhoneNum>9090909000</ContactPhoneNum>
//</root>

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

Create multiple XmlDocument objects based on the number of XML files in a directory

I have multiple XML files in a directory, Each directory you can consider as each scenario. Based on the business scenario number of XML files may differ in a directory.
From the Below code I got total number of files.
string tempPath = rootPath+"/Templates"; // Template directory path
DirectoryInfo d = new DirectoryInfo(#tempPath);
FileInfo[] files = d.GetFiles("*.xml"); // getting all file names
Am trying to set the namespace for the xml like the below code
//Setting namespace for each xml
foreach(FileInfo file in files)
{
var tempXml = file.Name;
XmlDocument tempXml = new XmlDocument();
tempXml.Load(tempPath+"/"+file.Name);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(tempXml.NameTable);
nsMgr.AddNamespace("imp", namespace1); // namespace1 value I took it from excel
nsMgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
}
Later that I trying to insert value in the xml doc like this
XmlNode businessContactNumber = doc.SelectSingleNode(xPath,nsMgr);
The above code is not working and I know am doing some serious mistake in the code. Am new to this C# code, Kindly help me to resolve this issue.
Using XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication62
{
class Program
{
const string FOLDER = #"\c:\temp";
static void Main(string[] args)
{
string[] filenames = Directory.GetFiles(FOLDER);
foreach (string file in filenames)
{
XDocument tempXml = XDocument.Load(file);
XElement root = (XElement)tempXml.FirstNode;
XNamespace nsMgr = root.Name.Namespace;
XElement node = root.Descendants(nsMgr + "TagName").FirstOrDefault();
}
}
}
}

Categories

Resources