Click here to Skip to main content
14,832,287 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am writing an application that converts XML file to TAR file. I tried using XmlReader for conversion. but the problem I got at "repairStatus" is in the content. There are 2 cases in the file are "Repaired" and "False Call" I cannot split them into separate lines in the TAR file. Can someone help me convert like below: XML file in folder C:\Logdata\XML with content:
XML
<ns1:BoardTestXMLExport xmlns:ns1="http://tempuri.org/BoardTestXMLExport.xsd" numberOfIndictedComponents="4" testerTestStartTime="2021-01-21T07:44:07.000+07:00" testTime="2021-01-21T07:44:07.000+07:00" repairStationId="HCMINGAOI14" testStatus="Repaired" testerTestEndTime="2021-01-21T07:44:11.000+07:00" numberOfIndictedPins="0" numberOfComponentsTested="367" numberOfJointsTested="0" numberOfDefects="4" repairStatus="Repaired">
<ns1:BoardXML imageId="1" serialNumber="21020215311" assemblyRevision="ING-296277001AD_MS-A-T" boardType="ING-296277001AD_MS-A-T" boardRevision="1609722598000"/>
<ns1:StationXML testerName="HCMINGAOI05" stage="V510"/>
<ns1:RepairEventXML numberOfVariationOkDefects="0" numberOfFalseCalledPins="0" numberOfRepairedComponents="2" numberOfVariationOkPins="0" numberOfRepairedPins="0" numberOfRepairLaterPins="0" numberOfFalseCalledDefects="2" numberOfActiveDefects="0" numberOfVariationOkComponents="0" repairEndTime="2021-01-21T07:46:02.000+07:00" repairStartTime="2021-01-21T07:45:50.000+07:00" numberOfRepairLaterDefects="0" repairOperator="c_admin" numberOfRepairLaterComponents="0" numberOfActiveComponents="0" numberOfActivePins="0" numberOfRepairedDefects="2" numberOfFalseCalledComponents="2"/>
<ns1:TestXML name="1:u302">
<ns1:IndictmentXML algorithm="s192059844" indictmentType="Billboarding">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-21T07:45:58.000+07:00" repairActionType="-" indictmentType="Billboarding" comment="-" repairStatus="Repaired"/>
<ns1:ComponentXML packageId="192059844" partId="192059844" designator="1:u302"/>
</ns1:IndictmentXML>
</ns1:TestXML>
<ns1:TestXML name="1:u1701">
<ns1:IndictmentXML algorithm="u1910000380" indictmentType="Missing">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-21T07:45:55.000+07:00" repairActionType="-" indictmentType="Missing" comment="-" repairStatus="Repaired"/>
<ns1:ComponentXML packageId="191000038" partId="191000038" designator="1:u1701"/>
</ns1:IndictmentXML>
</ns1:TestXML>
<ns1:TestXML name="1:d1701">
<ns1:IndictmentXML algorithm="bdiode-cr316" indictmentType="Wrong Polarity">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-21T07:45:51.000+07:00" repairActionType="-" indictmentType="Wrong Polarity" comment="-" repairStatus="False Call"/>
<ns1:ComponentXML packageId="188986853" partId="188986853" designator="1:d1701"/>
</ns1:IndictmentXML>
</ns1:TestXML>
<ns1:TestXML name="1:c432">
<ns1:IndictmentXML algorithm="c192029309" indictmentType="Insuff Solder">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-21T07:45:50.000+07:00" repairActionType="-" indictmentType="Insuff Solder" comment="-" repairStatus="False Call"/>
<ns1:ComponentXML packageId="192029309" partId="192029309" designator="1:c432"/>
</ns1:IndictmentXML>
</ns1:TestXML>
</ns1:BoardTestXMLExport>


What I have tried:

Someone help me convert to text with format below using c#.thanks!

S21020215311
CHCM_INGENICO
NHCMINGAOI05_T
PQC
n296277001AD.MS
rA
Oc_admin
L4
p1
[2021-01-21 07:44:07
]2021-01-21 07:44:11
TF
F,u302,u1701
>Failed
~
cc905
AUpside down
(2021-01-21 07:44:11
~
~
cu302
ABillboarding
(2021-01-21 07:44:11
~
~
cu1701
AMissing
(2021-01-21 07:44:11
~
Posted
Updated 23-Jan-21 3:36am
v2
Comments
Richard MacCutchan 21-Jan-21 8:04am
   
A tar file is just an archive of other files. The content can be anything. Just pass the data as text.

You can generate classes here: Xml2CSharp.com | Convert your XML Examples into XmlSerializer compatible C# Classes[^]
The rest is up to you :)

Example:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace Xml2CSharp
{
	[XmlRoot(ElementName="BoardXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
	public class BoardXML {
		[XmlAttribute(AttributeName="imageId")]
		public string ImageId { get; set; }
		[XmlAttribute(AttributeName="serialNumber")]
		public string SerialNumber { get; set; }
		[XmlAttribute(AttributeName="assemblyRevision")]
		public string AssemblyRevision { get; set; }
		[XmlAttribute(AttributeName="boardType")]
		public string BoardType { get; set; }
		[XmlAttribute(AttributeName="boardRevision")]
		public string BoardRevision { get; set; }
	}

	[XmlRoot(ElementName="StationXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
	public class StationXML {
		[XmlAttribute(AttributeName="testerName")]
		public string TesterName { get; set; }
		[XmlAttribute(AttributeName="stage")]
		public string Stage { get; set; }
	}

	[XmlRoot(ElementName="RepairEventXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
	public class RepairEventXML {
		[XmlAttribute(AttributeName="numberOfVariationOkDefects")]
		public string NumberOfVariationOkDefects { get; set; }
		[XmlAttribute(AttributeName="numberOfFalseCalledPins")]
		public string NumberOfFalseCalledPins { get; set; }
		[XmlAttribute(AttributeName="numberOfRepairedComponents")]
		public string NumberOfRepairedComponents { get; set; }
		[XmlAttribute(AttributeName="numberOfVariationOkPins")]
		public string NumberOfVariationOkPins { get; set; }
		[XmlAttribute(AttributeName="numberOfRepairedPins")]
		public string NumberOfRepairedPins { get; set; }
		[XmlAttribute(AttributeName="numberOfRepairLaterPins")]
		public string NumberOfRepairLaterPins { get; set; }
		[XmlAttribute(AttributeName="numberOfFalseCalledDefects")]
		public string NumberOfFalseCalledDefects { get; set; }
		[XmlAttribute(AttributeName="numberOfActiveDefects")]
		public string NumberOfActiveDefects { get; set; }
		[XmlAttribute(AttributeName="numberOfVariationOkComponents")]
		public string NumberOfVariationOkComponents { get; set; }
		[XmlAttribute(AttributeName="repairEndTime")]
		public string RepairEndTime { get; set; }
		[XmlAttribute(AttributeName="repairStartTime")]
		public string RepairStartTime { get; set; }
		[XmlAttribute(AttributeName="numberOfRepairLaterDefects")]
		public string NumberOfRepairLaterDefects { get; set; }
		[XmlAttribute(AttributeName="repairOperator")]
		public string RepairOperator { get; set; }
		[XmlAttribute(AttributeName="numberOfRepairLaterComponents")]
		public string NumberOfRepairLaterComponents { get; set; }
		[XmlAttribute(AttributeName="numberOfActiveComponents")]
		public string NumberOfActiveComponents { get; set; }
		[XmlAttribute(AttributeName="numberOfActivePins")]
		public string NumberOfActivePins { get; set; }
		[XmlAttribute(AttributeName="numberOfRepairedDefects")]
		public string NumberOfRepairedDefects { get; set; }
		[XmlAttribute(AttributeName="numberOfFalseCalledComponents")]
		public string NumberOfFalseCalledComponents { get; set; }
	}

	[XmlRoot(ElementName="RepairActionXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
	public class RepairActionXML {
		[XmlAttribute(AttributeName="repairOperator")]
		public string RepairOperator { get; set; }
		[XmlAttribute(AttributeName="repairTime")]
		public string RepairTime { get; set; }
		[XmlAttribute(AttributeName="repairActionType")]
		public string RepairActionType { get; set; }
		[XmlAttribute(AttributeName="indictmentType")]
		public string IndictmentType { get; set; }
		[XmlAttribute(AttributeName="comment")]
		public string Comment { get; set; }
		[XmlAttribute(AttributeName="repairStatus")]
		public string RepairStatus { get; set; }
	}

	[XmlRoot(ElementName="ComponentXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
	public class ComponentXML {
		[XmlAttribute(AttributeName="packageId")]
		public string PackageId { get; set; }
		[XmlAttribute(AttributeName="partId")]
		public string PartId { get; set; }
		[XmlAttribute(AttributeName="designator")]
		public string Designator { get; set; }
	}

	[XmlRoot(ElementName="IndictmentXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
	public class IndictmentXML {
		[XmlElement(ElementName="RepairActionXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
		public RepairActionXML RepairActionXML { get; set; }
		[XmlElement(ElementName="ComponentXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
		public ComponentXML ComponentXML { get; set; }
		[XmlAttribute(AttributeName="algorithm")]
		public string Algorithm { get; set; }
		[XmlAttribute(AttributeName="indictmentType")]
		public string IndictmentType { get; set; }
	}

	[XmlRoot(ElementName="TestXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
	public class TestXML {
		[XmlElement(ElementName="IndictmentXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
		public IndictmentXML IndictmentXML { get; set; }
		[XmlAttribute(AttributeName="name")]
		public string Name { get; set; }
	}

	[XmlRoot(ElementName="BoardTestXMLExport", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
	public class BoardTestXMLExport {
		[XmlElement(ElementName="BoardXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
		public BoardXML BoardXML { get; set; }
		[XmlElement(ElementName="StationXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
		public StationXML StationXML { get; set; }
		[XmlElement(ElementName="RepairEventXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
		public RepairEventXML RepairEventXML { get; set; }
		[XmlElement(ElementName="TestXML", Namespace="http://tempuri.org/BoardTestXMLExport.xsd")]
		public List<TestXML> TestXML { get; set; }
		[XmlAttribute(AttributeName="ns1", Namespace="http://www.w3.org/2000/xmlns/")]
		public string Ns1 { get; set; }
		[XmlAttribute(AttributeName="numberOfIndictedComponents")]
		public string NumberOfIndictedComponents { get; set; }
		[XmlAttribute(AttributeName="testerTestStartTime")]
		public string TesterTestStartTime { get; set; }
		[XmlAttribute(AttributeName="testTime")]
		public string TestTime { get; set; }
		[XmlAttribute(AttributeName="repairStationId")]
		public string RepairStationId { get; set; }
		[XmlAttribute(AttributeName="testStatus")]
		public string TestStatus { get; set; }
		[XmlAttribute(AttributeName="testerTestEndTime")]
		public string TesterTestEndTime { get; set; }
		[XmlAttribute(AttributeName="numberOfIndictedPins")]
		public string NumberOfIndictedPins { get; set; }
		[XmlAttribute(AttributeName="numberOfComponentsTested")]
		public string NumberOfComponentsTested { get; set; }
		[XmlAttribute(AttributeName="numberOfJointsTested")]
		public string NumberOfJointsTested { get; set; }
		[XmlAttribute(AttributeName="numberOfDefects")]
		public string NumberOfDefects { get; set; }
		[XmlAttribute(AttributeName="repairStatus")]
		public string RepairStatus { get; set; }
	}
}
   
Comments
Sơ Mi Tv 23-Jan-21 6:41am
   
I still don't understand. If create such a class, where does the input file xml? Namespace = "http://tempuri.org/BoardTestXMLExport.xsd" =>? You can help me improve the code?
This shows how you can get the values:
//using System.Linq;
//using System.Xml.Linq;

XDocument xdoc = XDocument.Load("MyFile.xml");

foreach (XElement node in xdoc.Root.Descendants())
{
    Debug.Print(node.Name.LocalName);

    foreach (var attr in node.Attributes())
    {
        Debug.Print("    " + attr.Name + " = " + attr.Value);
    }
}

You might also be interested in:
best-resources-for-a-programming-beginner-to-learn-c[^]
   
v2
Comments
Sơ Mi Tv 24-Jan-21 7:22am
   
hi @RickZeeland tried creating a New Class with the name ConverXML. In the form, I call the class but the value is null because the class is not a file string. I want to ask with the new class how can it read the XML file in the specified Folder?

```
RepairEventXML xml = new RepairEventXML();
sw.WriteLine(xml.NumberOfActiveComponents);
```
RickZeeland 24-Jan-21 7:36am
   
As you can see in the above example you can also get the text without using any classes, this is probably the simplest way for you.
Sơ Mi Tv 24-Jan-21 7:56am
   
I am a beginner, with such text format can you help me convert?
'''
S21020215311
CHCM_INGENICO
NHCMINGAOI05_T
PQC
n296277001AD.MS
rA
Oc_admin
L4
p1
[2021-01-21 07:44:07
]2021-01-21 07:44:11
TF
F,u302,u1701
>Failed
~
cc905
AUpside down
(2021-01-21 07:44:11
~
~
cu302
ABillboarding
(2021-01-21 07:44:11
~
~
cu1701
AMissing
(2021-01-21 07:44:11
~
'''
RickZeeland 24-Jan-21 8:43am
   
From the above example you only need the attr.Value to display the values you want.
Sơ Mi Tv 24-Jan-21 8:57am
   
Ok special thanks bro

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900