Hi
ich muss eine XML datei erzeugen.
bisheriger code:
string batchnr = "2005368";
string exp = "2023-07-26";
string mad = "2020-07-26";
string gtin = "06221032240262";
string workorder_code = "242040_2005368_1";
string sn = "200536810100508";
string status = "Accepted";
XNamespace xsiNs = "http://www.w3.org/2001/XMLSchema-instance";
XDocument xDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", "no"),
new XElement("PRODUCTION_IMPORTATION",
new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
new XAttribute(xsiNs + "noNamespaceSchemaLocation", "Batch.xsd"),
new XElement("BATCH",
new XAttribute("BATCH_NUMBER", batchnr),
new XAttribute("MANUFACTURE_DATE", mad),
new XAttribute("EXPIRATION_DATE", exp),
new XElement("WORKORDER",
new XAttribute("GTIN", gtin),
new XAttribute("WORKORDER_CODE", workorder_code)
)
)
));
xDoc.Root.Add(new XElement("SERIALIZED_ITEM",
new XAttribute("SERIAL", sn),
new XAttribute("Status", status)
));
xDoc.Root.Add(new XElement("SERIALIZED_ITEM",
new XAttribute("SERIAL", sn),
new XAttribute("Status", status)
));
xDoc.Save("text.xml");
ergebnis:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<PRODUCTION_IMPORTATION xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Batch.xsd">
<BATCH BATCH_NUMBER="2005368" MANUFACTURE_DATE="2020-07-26" EXPIRATION_DATE="2023-07-26">
<WORKORDER GTIN="06221032240262" WORKORDER_CODE="242040_2005368_1" />
</BATCH>
<SERIALIZED_ITEM SERIAL="200536810100508" Status="Accepted" />
<SERIALIZED_ITEM SERIAL="200536810100508" Status="Accepted" />
</PRODUCTION_IMPORTATION>
das ist leider nur fast richtig...
die
<SERIALIZED_ITEM SERIAL="200536810100508" Status="Accepted" />
<SERIALIZED_ITEM SERIAL="200536810100508" Status="Accepted" />
sollen innerhalb des WORKORDER stehen.
wie bekomm ich die also höher in die xml rein?
das eigentliche ziel ist das:
<?xml version="1.0" encoding="utf-8"?>
<PRODUCTION_IMPORTATION xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Batch.xsd">
<BATCH BATCH_NUMBER="2005368" MANUFACTURE_DATE="2020-07-26" EXPIRATION_DATE="2023-07-26">
<WORKORDER GTIN="06221032240262" WORKORDER_CODE="242040_2005368_1">
<SERIALIZED_ITEM SERIAL="200536810100508" Status="Accepted"/>
<SERIALIZED_ITEM SERIAL="200536810100516" Status="Accepted"/>
</WORKORDER>
</BATCH>
</PRODUCTION_IMPORTATION>
mfg kala
du schreibst die einträge in die root und daher landen sie auch in die root du musst schon das richtige element verwenden von alleine passiert das nicht!!
die moderatoren haben dich schon auf die grundlagen von xml mit c# hingewiesen wieso beachtest du das nicht???
https://www.mycsharp.de/wbb2/thread.php?threadid=123499
Für "BATCH"
hast du doch auch das Unterelement "WORKORDER"
erstellt. Genauso kannst du es für "SERIALIZED_ITEM"
erzeugen.
hi
ich muss eine forschleife um
xDoc.Root.Add(new XElement("SERIALIZED_ITEM",
new XAttribute("SERIAL", sn),
new XAttribute("Status", status)
));
setzen, da ich die elemente dynamisch aus einer DB in ein File schreiben will und das geht nicht innerhalb des 1. befehls. also hab ich mittels dem Add versucht dies hinzuzufügen. Mir war fast klar, das .Root das falsche ist, aber nur .Add hat nicht funktioniert.
Wie gebe ich also an, wenn ich den 1. Befehl so hab, das das, was in meiner For-Schleife an die richtige Position kommt, das hab ich bisher in den Dokus die ich gelesen hab, nicht gefunden.
Mfg Kala
Dann erstelle dir für new XElement("WORKORDER", ...)
eine Variable, füge dann in einer Schleife die SERIALIZED_ITEM
-Einträge hinzu und verwende dann diese Variable in der XDocument
-Anweisung.
ok habs selbst raus gefunden...
private void btnTest_Click(object sender, EventArgs e)
{
string batchnr = "2005368";
string exp = "2023-07-26";
string mad = "2020-07-26";
string gtin = "06221032240262";
string workorder_code = "242040_2005368_1";
string[] sn = {"200536810100508", "200536810100509", "200536810100510"};
string status = "Accepted";
XElement xmlTree1 = new XElement("SERIALIZED_ITEM");
for (int i = 0; i < sn.Length; i++)
{
xmlTree1.Add(new XElement("SERIALIZED_ITEM",
new XAttribute("SERIAL", sn[i]),
new XAttribute("Status", status)));
}
XNamespace xsiNs = "http://www.w3.org/2001/XMLSchema-instance";
XDocument xDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", ""),
new XElement("PRODUCTION_IMPORTATION",
new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
new XAttribute(xsiNs + "noNamespaceSchemaLocation", "Batch.xsd"),
new XElement("BATCH",
new XAttribute("BATCH_NUMBER", batchnr),
new XAttribute("MANUFACTURE_DATE", mad),
new XAttribute("EXPIRATION_DATE", exp),
new XElement("WORKORDER",
new XAttribute("GTIN", gtin),
new XAttribute("WORKORDER_CODE", workorder_code),
from el in xmlTree1.Elements()
select el
)
)
)
);
xDoc.Save("text.xml");
}
nur noch eine frage der verständnis halber:
warum muss ich
XElement xmlTree1 = new XElement("SERIALIZED_ITEM");
dieses element erzeugen?
eigentlich adde ich doch das hinzu.
Steht doch da:
new XElement("WORKORDER",
new XAttribute("GTIN", gtin),
new XAttribute("WORKORDER_CODE", workorder_code),
from el in xmlTree1.Elements()
select el
Könntest natürlich auch anders machen 😉
Ist immer gut wenn man vom eigenen Code keine Ahnung hat was passiert 😄
Dein Code ist etwas umständlich:
from el in xmlTree1.Elements()
select el
kann man verkürzen zu
xmlTree1.Elements()
Da du auf diese Elemente zugreifst, brauchst du ein Container-Objekt (bei dir hier xmlTree1
).
Es ginge jedoch auch einfach eine List<XElement>
o.ä.
Jedoch ist dein Code performancetechnisch etwas schlechter als mein vorgeschlagener Weg, da du erst eine Liste erzeugst und diese dann iteriert wird, um die Sub-Elemente zu erzeugen - anstatt gleich die Sub-Elemente an das passende Element hinzuzufügen.