Remove xml:space="preserve" of XML file using C# - c#

I used this code to export to XML file from DataTable:
dt1 = TNET2_POHeadService.TNET2_POHead_GetByPO_NoRaw(PO_No);
dt2 = TNET2_PODetailsService.TNET2_PODetails_GetByPO_NoRaw(PO_No);
ds.Tables.Add(dt1);
ds.Tables[0].TableName = "TNET2_POHead";
ds.Tables.Add(dt2);
ds.Tables[1].TableName = "TNET2_PODetails";
saveFileDialog1.ShowDialog();
ds.WriteXml(saveFileDialog1.FileName);
Everything worked fine but :
<ContractNo>P1717-198905-003(01)</ContractNo>
<KP_No xml:space="preserve"> </KP_No>
<SettlementRoute1>TENTAC SUZHOU</SettlementRoute1>
KP_No tag is blank value. I want to remove xml:space="preserve" of the XML export file. How to do that?

The XDocument class in the System.Xml.Linq namespace allows for easy manage and manipulation of XML using LINQ.
See example below to remove the xml:space="preserve" attribute from the KP_No element of the XML file:
XDocument doc = XDocument.Load("XMLFile1.xml"); // or XDocument.Load(myStream) ...
Func<XAttribute, bool> preserveAttrFunc = atr => atr.Name.LocalName == "space" && atr.Value == "preserve";
XElement kp_no = doc.Descendants("KP_No").FirstOrDefault(kp => kp.HasAttributes && kp.Attributes().Any(preserveAttrFunc));
if(kp_no != null)
kp_no.Attributes().FirstOrDefault(preserveAttrFunc).Remove();
// doc.Save(filename) ...
Note your XML doesn´t have a root element. It's good practice to have one defined in XML.
Result after the code snippet above:
<root>
<ContractNo>P1717-198905-003(01)</ContractNo>
<KP_No></KP_No>
<SettlementRoute1>TENTAC SUZHOU</SettlementRoute1>
</root>

Related

How to get the name of elements/attributes in xml files and write these into another new xml file using LINQ

I am new to c#. Right now I am trying to get the name of some specific elements in an xml file and create a new xml file for these.
I have tried with LINQ, so that I can parse the xml and get the name of some elments that I want.
I do not want to use console.writeline to output this name. Instead I would like to create a new xml file and save these names in it.
Can anyone give me some tips?
The following is example data I would like to parse:
<root>
<Package>
<Class name="EnterpriseArchitect">
<Operation/>
</Class>
<Class name="ZachmanFramework">
<Operation/>
</Class>
</Package>
</root>
I want to get the attribute name of the element Class and save it in a new xml file like this:
<root>
<EnterpriseArchitect/>
<ZachmanFramework/>
</root>
The following is my c# code, but I can not reach the goal:
XDocument xdoc = XDocument.Load(#"C:\Users\jsc\Desktop\123456.xml");
XDocument xNew = new XDocument();
var datatype = xdoc.Root.Elements("Package").Elements("Class")
foreach (var dut in datatype)
{
var dataTypeName = dut.Attribute("name").Value;
xNew.Add(new XElement(dataTypeName));
}
xNew.Save(#"C:\Users\jsc\Desktop\1234567.xml");
Please, read my comments to the question.
This should work:
XDocument srcdoc = XDocument.Load("sourceFileName.xml");
List<XElement> names = srcdoc.Descendants("Class")
.Select(x=> new XElement(x.Attribute("name").Value))
//.Distinct() //uncomment this if duplicate values aren't allowed
.ToList();
XDocument newdoc = new XDocument();
XElement root = new XElement("root");
root.Add(names);
newdoc.Add(root);
newdoc.Save("newFileName.xml");
Good luck!

Checking for Existing XML Element Content and Replacing It

I have a c# app that writes to XML using the following code:
//Write last compliant elements to state XML if allCompliant bool == true
if (allCompliant == true)
{
if (File.Exists(MainEntry.thirdPartyStateXMLPath))
{
XDocument doc = XDocument.Load(MainEntry.thirdPartyStateXMLPath);
DateTime localCurrentTime = DateTime.Now;
DateTime utcCurrentTime = DateTime.UtcNow;
XElement root = new XElement("Compliance_Status");
root.Add(new XElement("Last_Known_Compliant_UTC", utcCurrentTime));
root.Add(new XElement("Last_Known_Compliant_LocalTime", localCurrentTime.ToString()));
doc.Element("Compliance_Items").Add(root);
doc.Save(MainEntry.thirdPartyStateXMLPath);
}
}
That renders the following XML:
<?xml version="1.0" encoding="utf-8"?>
<Compliance_Items>
<Compliance_Status>
<Last_Known_Compliant_UTC>2014-04-03T23:22:31.507088Z</Last_Known_Compliant_UTC>
<Last_Known_Compliant_LocalTime>4/3/2014 4:22:31 PM</Last_Known_Compliant_LocalTime>
</Compliance_Status>
</Compliance_Items>
This code generates the *Last_Known_Compliant_UTC* and *Last_Known_Compliant_LocalTime* elements and values. On subsequent runs of the code I want it to only replace the values of the existing elements, but as written now the following is re-created each time and keeps stacking in the XML:
<Compliance_Status>
<Last_Known_Compliant_UTC>2014-04-03T23:22:31.507088Z</Last_Known_Compliant_UTC>
<Last_Known_Compliant_LocalTime>4/3/2014 4:22:31 PM</Last_Known_Compliant_LocalTime>
</Compliance_Status>
How can I achieve the desired effect?
If you want to get a specific item and modify it you can do the following:
var xmlDocument = XDocument.Load("path");
var element = xmlDocument
.Descendants("Compliance_Status")
.FirstOrDefault(x => (DateTime)x.Element("Last_Known_Compliant_UTC") == someValue)
if(element != null)
/* update the value simply using element.Value = something
and save the xml file xmlDocument.Save("path") */
If you want to replace your element with another element you can use XElement.ReplaceWith method.

Deleting node from string with xml structure

I have an string parameter with xml content in it. Basically the string have an XML inside.
string S = funcThatReturnsXML (parameters);
S have the next text:
<?xml version="1.0" encoding="utf-8" ?>
<tagA>
<tagB>
<tagBB>
..
.
.
</tagBB>
.
.
</tagB>
<tagC>
..
..
.
</tagC>
</tagA>
The funcThatReturnsXML (parameters) creates an XmlDocument object but the return it as a string, I cant change this function, to much stuff works with it.
Tried to create XmlDocument objetc but the SelectSingleNode return null.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(S);
XmlNode root = xmlDoc.SelectSingleNode("tagB");
How can I delete from string S (not XML Object) specific node, for example <tagB>
EDIT: this is the XML I tested with:
<?xml version="1.0" ?>
- <Request xmlns:xsi="http://www.mysite.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <info xmlns="http://www.mysite.com">
<RequestTR>54</RequestTR>
<time>2013-12-22</time>
</info>
- <Parameters xmlns="http://www.mysite.com">
<id>3</id>
<name>2</name>
</Parameters>
<title>Request</title>
</Request>
Try this:
string S = funcThatReturnsXML(parameters);
var doc = XDocument.Parse(S);
var nodeToRemove = doc.Descendants("tagB");
nodeToRemove.Remove();
That will remove all nodes named "tagB" from string S which contains xml.
UPDATE 1:
Sorry, i missed to include one more line:
S = doc.ToString();
My first code above removed "tagB" from doc but didnt save it back to S variable.
UPDATE 2:
I tested with following xml which contain attribute:
<tagA attribute="value">
<tagB>
<tagBB>
</tagBB>
</tagB>
<tagC></tagC>
</tagA>
and the output of Console.WriteLine(S):
<tagA attribute="value">
<tagC></tagC>
</tagA>
UPDATE 3:
Given your updated xml format, I know why my previous code didn't work for you. That was because your xml have namespace (xmlns) declared. The solution is to use LocalName when searching for the node to be removed, that will search for node name while ignoring its namespace. The follwoing example shows how to remove all "info" node:
var doc = XDocument.Parse(S);
var nodeToRemove = doc.Descendants().Where(o => o.Name.LocalName == "info");
nodeToRemove.Remove();
S = doc.ToString();
If you can determine the particular outer element to remove from the returned XML, you could use LINQ to XML:
var returnedXml = funcThatReturnsXML(parameters);
var xmlElementToRemove = funcThatReturnsOuterElement(returnedXml);
var xelement = XElement.Load("XmlDoc.txt");
xelement.Elements().Where(e => e.Name == xmlElementToRemove).Remove();
For example:
using System.Linq;
using System.Xml.Linq;
class Program
{
static void Main(string[] args)
{
// pretend this is the funThatReturnsXML return value
var returnedXml = "<tagB><tagBB></tagBB></tagB>";
// get the outer XML element name
var xmlElementToRemove = GetOuterXmlElement(returnedXml);
// load XML from where ever
var xelement = XElement.Load("XmlDoc.txt");
// remove the outer element and all subsequent elements
xelement.Elements().Where(e => e.Name == xmlElementToRemove).Remove();
}
static string GetOuterXmlElement(string xml)
{
var index = xml.IndexOf('>');
return xml.Substring(1, index - 1);
}
}
Note that the above is a "greedy" removal method, if there is more than once element with the name returned via the GetOuterXmlElemet method they will all be removed. If you want a specific instance to be removed then you will require something more sophisticated.
Building on your edit:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(S);
var nodeA = xmlDoc.SelectSingleNode("/tagA");
var nodeB = nodeA.SelectSingleNode("tagB");
nodeA.RemoveChild(nodeB);
To remove (possibly) multiple tagB nodes in unknown positions, you may try:
var bees = xmlDoc.SelectNodes("//tagB");
foreach (XmlNode bee in bees) {
var parent = bee.ParentNode;
parent.RemoveChild(bee);
}

xml Nodes by Element

Below is an example of the xml file that I need to pull data via C#. This is my first experience with reading xml files and a beginner with xml. Anyone have an example of how I would find/load the fieldorder values for Export_B?
<?xml version="1.0" encoding="utf-8"?>
<Config>
<OutFolderCSV>c:\Output\2012\upload_Files</OutFolderCSV>
<OutFolderImage>c:\Output\2012\NM_Scorecard_Images</OutFolderImage>
<PathOutLogFile>c:\Output\2012\Log\Matches.log</PathOutLogFile>
<FieldSeparator>,</FieldSeparator>
<ExportFile>
<Name>Export_A</Name>
<FieldOrder>matchID</FieldOrder>
<FieldOrder>contactID</FieldOrder>
<FieldOrder>stageID13</FieldOrder>
<FieldOrder>stringScore1a</FieldOrder>
<FieldOrder>xScore1a</FieldOrder>
<FieldOrder>stageID14</FieldOrder>
<FieldOrder>stringScore1b</FieldOrder>
<FieldOrder>xScore1b</FieldOrder>
<FieldOrder>stageID15</FieldOrder>
<FieldOrder>stringScore1c</FieldOrder>
<FieldOrder>xScore1c</FieldOrder>
</ExportFile>
<ExportFile>
<Name>Export_B</Name>
<FieldOrder>matchID</FieldOrder>
<FieldOrder>contactID</FieldOrder>
<FieldOrder>stageID16</FieldOrder>
<FieldOrder>stringScore1a</FieldOrder>
<FieldOrder>xScore1a</FieldOrder>
<FieldOrder>stageID17</FieldOrder>
<FieldOrder>stringScore1b</FieldOrder>
<FieldOrder>xScore1b</FieldOrder>
<FieldOrder>stageID18</FieldOrder>
<FieldOrder>stringScore1c</FieldOrder>
<FieldOrder>xScore</FieldOrder>
</ExportFile>
</Config>
Using LINQ to XML:
var doc = XDocument.Load(#"c:\path\to\file.xml");
var fieldOrders =
from exportFile in doc.Descendants("ExportFile")
where (string)exportFile.Element("Name") == "Export_B"
from fieldOrder in exportFile.Elements("FieldOrder")
select (string)fieldOrder;
I have written an article
http://www.codeproject.com/Articles/33769/Basics-of-LINQ-Lamda-Expressions
on XML using XDocument object.
You can parse the XML easily using
XDocument.Load(filepath)
Please read the section XLinq to parse the objects.
edit :
You can change value of Export_B using the code :
var document = XDocument.Load(filepath)
var exportFiles = document.Descandants("ExportFile");
List<XElement> list = new List<XElement>();
foreach(var element in exportFiles)
{
list.Add(element);
// Now you can do element.Element("Name") to get the name. Put a breakpoint on this, you can get the reference of all underlying objects.
}

I am trying to read directory from xml file in c# and have problem

<?xml version="1.0" encoding="UTF-8"?>
<form:Documents xmlns:form="http://www.abbyy.com/FlexiCapture/Schemas/Export/FormData.xsd" xmlns:addData="http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd">
<_Document_Definition_1:_Document_Definition_1 addData:ImagePath="C:\POC\Export\Test.pdf" xmlns:_Document_Definition_1="http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd">
<_Page_1>
<_First_Name>John</_First_Name>
<_Last_Name>Doe</_Last_Name>
</_Page_1>
</_Document_Definition_1:_Document_Definition_1>
</form:Documents>
I have xml containing directory of pdf file which I would need to read.
I can read first name and last name from _Page_1 node but do not know how to read ImagePath.
Here is my code to read from _Page_1
XDocument xDoc = XDocument.Load("Test.xml");
var poc = from p in xDoc.Descendants("_Page_1")
select new
{
FirstName = p.Element("_First_Name").Value,
LastNumber = p.Element("_Last_Name").Value
};
// Execute the query
foreach (var customer in poc)
{
Console.WriteLine(customer.FirstName);
Console.WriteLine(customer.LastName);
}
//Pause the application
Console.ReadLine();
Thank you BrokenGlass, it's working.
I have one more question.
What if I have several iteration of _Document_Definition node, how do I read each iteration.
<?xml version="1.0" encoding="UTF-8"?>
<form:Documents xmlns:form="http://www.abbyy.com/FlexiCapture/Schemas/Export/FormData.xsd" xmlns:addData="http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd">
<_Document_Definition_1:_Document_Definition_1 addData:ImagePath="C:\POC\Export\Test.pdf" xmlns:_Document_Definition_1="http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd">
<_Page_1>
<_First_Name>John</_First_Name>
<_Last_Name>Doe</_Last_Name>
</_Page_1>
</_Document_Definition_1:_Document_Definition_1>
<_Document_Definition_1:_Document_Definition_1 addData:ImagePath="C:\POC\Export\Test2.pdf" xmlns:_Document_Definition_1="http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd">
<_Page_1>
<_First_Name>Jane</_First_Name>
<_Last_Name>Doe</_Last_Name>
</_Page_1>
</_Document_Definition_1:_Document_Definition_1>
</form:Documents>
You are missing the XML namespace references to access those attributes, this works:
XDocument doc = XDocument.Load(#"test.xml");
XNamespace _Document_Definition_1 = "http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd";
XNamespace addData = "http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd";
string impagePath = doc.Descendants(_Document_Definition_1 + "_Document_Definition_1")
.First()
.Attribute(addData + "ImagePath")
.Value;
It looks like Imagepath is an attribute not an element. Hence you are not able to read it. Check for the attributes in the xml file.

Categories