I have file XML with content:
<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>
and a need convert to text file with format:
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
~
with the tag that will be added automatically as user input. I have tried with the following code:
public void RbtnXml_CheckedChanged(object sender, EventArgs e)
{
if (RbtnXml.Checked == true)
{
pcXML.Image = Image.FromFile(@"Resourcesxml.png");
DateTime dateTime = DateTime.Now;
string[] filenames = Directory.GetFiles(txtSource.Text, "*.xml");
foreach (string filename in filenames)
{
XDocument doc = XDocument.Load(filename);
XElement BoardTestXMLExport = doc.Root;
XNamespace ns1 = BoardTestXMLExport.GetNamespaceOfPrefix("ns1");
int numberOfIndictedComponents = (int)BoardTestXMLExport.Attribute("numberOfIndictedComponents");
DateTime testerTestStartTime = (DateTime)BoardTestXMLExport.Attribute("testerTestStartTime");
DateTime testTime = (DateTime)BoardTestXMLExport.Attribute("testTime");
string testStatus = (string)BoardTestXMLExport.Attribute("testStatus");
DateTime testerTestEndTime = (DateTime)BoardTestXMLExport.Attribute("testerTestEndTime");
XElement BoardXML = BoardTestXMLExport.Element(ns1 + "BoardXML");
string serialNumber = (string)BoardXML.Attribute("serialNumber");
string assemblyRevision = (string)BoardXML.Attribute("assemblyRevision");
string boardType = (string)BoardXML.Attribute("boardType");
string boardRevision = (string)BoardXML.Attribute("boardRevision");
XElement StationXML = BoardTestXMLExport.Element(ns1 + "StationXML");
string testerName = (string)StationXML.Attribute("testerName");
XElement RepairEventXML = BoardTestXMLExport.Element(ns1 + "RepairEventXML");
string Operator = (string)RepairEventXML.Attribute("repairOperator");
{
string nameFile = Path.GetFileNameWithoutExtension(filename);
using (StreamWriter sw = File.CreateText(txtParser.Text + nameFile + ".tar"))
if (testStatus.Equals("Repaired"))
{
sw.WriteLine("S" + serialNumber);
sw.WriteLine("C" + cbWorkcell.Text);
sw.WriteLine("N" + testerName);
sw.WriteLine("PQC");
sw.WriteLine("n" + assemblyRevision.Substring(4, 11));
sw.WriteLine("rA");
sw.WriteLine("O" + Operator);
sw.WriteLine("L4");
sw.WriteLine("p1");
sw.WriteLine("[" + dateTime.AddSeconds(-10).ToString("yyyy-MM-dd HH:mm:ss"));
sw.WriteLine("]" + dateTime.ToString("yyyy-MM-dd HH:mm:ss"));
sw.WriteLine("FT");
var result = string.Empty;
foreach (XElement ComponentXML in doc.Descendants(ns1 + "ComponentXML"))
foreach (var cRD in ComponentXML.Attributes("designator"))
result += "," + $"{cRD.Value.Substring(2)}";
sw.WriteLine("F"+result);
sw.WriteLine(">Failed");
sw.WriteLine("~");
foreach (XElement IndictmentXML in doc.Descendants(ns1 + "IndictmentXML"))
{
string indictmentType = (string)IndictmentXML.Attribute("indictmentType");
sw.WriteLine("c" + indictmentType);
}
}
else
{
sw.WriteLine("S" + serialNumber);
sw.WriteLine("C" + cbWorkcell.Text);
sw.WriteLine("N" + testerName);
sw.WriteLine("PQC");
sw.WriteLine("TP");
sw.WriteLine("n" + assemblyRevision.Substring(4, 11));
sw.WriteLine("rA");
sw.WriteLine("O" + Operator);
sw.WriteLine("L4");
sw.WriteLine("p1");
sw.WriteLine("[" + dateTime.AddSeconds(-10).ToString("yyyy-MM-dd HH:mm:ss"));
sw.WriteLine("]" + dateTime.ToString("yyyy-MM-dd HH:mm:ss"));
}
if (!Directory.Exists(@"HistoryXML"))
{
GetCreateMyFolder(@"HistoryXML");
}
}
}
}
else
{
pcXML.Image = Image.FromFile(@"Resourcesxml1.png");
}
}
But the result is repeated many times leading to the wrong format:
FT
Fu302
>Failed
~
cu302
ABillboarding
(2021-01-25 01:09:27
~
~
cu302
AMissing
(2021-01-25 01:09:27
~
~
cu302
AWrong Polarity
(2021-01-25 01:09:27
~
~
cu302
AInsuff Solder
(2021-01-25 01:09:27
~
Fu1701
>Failed
~
cu1701
ABillboarding
(2021-01-25 01:09:27
~
~
cu1701
AMissing
(2021-01-25 01:09:27
~
~
cu1701
AWrong Polarity
(2021-01-25 01:09:27
~
~
cu1701
AInsuff Solder
(2021-01-25 01:09:27
~
Fd1701
>Failed
~
cd1701
ABillboarding
(2021-01-25 01:09:27
~
~
cd1701
AMissing
(2021-01-25 01:09:27
~
In XML file total 4 defect ( 2 "Repaired", 2 "False Call"). I hope 2 "Repaired" will output: F,u302,u1701
someone please help fix code or advice for me. thanks
question from:
https://stackoverflow.com/questions/65874330/convert-xml-files-to-text-files-with-xdocument 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…