C# - Schreiben von XML-Daten über DOM

FlashCool

Grünschnabel
Hallo zusammen,

ich möchte unter C# mit DOM eine XML-Datei erstellen erhalte jedoch bei Ausführung eine Fehlermeldung
"System.InvalidOperationException".
Folgende Struktur möchte ich dabei erzeugen:


<?xml version="1.0" standalone="yes"?>
<DeliveryList>
<DeliveryNote>
<DocNo>9393</DocNo>
<Item>
<ItemNo>092309765<\ItemNo>
<Qty>36<\Qty>
<\Item>
<Item>
<ItemNo>09230923<\ItemNo>
<Qty>28<\Qty>
<\Item>
<Signature>97z3298zr2398z<\Signature>
</DeliveryNote>
</DeliveryList>


Mein Quellcode dazu sieht so aus:


public bool writeXML(DataSet dsDN, string DocNo, byte[] Signature)
{
string FileName = DocNo;
string FileExt = ".xml";
string FilePath = Globals.ClientOutBoxPath;

XmlProcessingInstruction XMLIns;
XmlNode XMLNodeIns, XMLNodeDN, XMLNodeDocNo, XMLNodeSignature;
XmlNode XMLNodeItem, XMLNodeItemNo, XMLNodeQty;
XmlElement XMLElem, XMLElemDNList;

XmlDocument XMLDoc = new XmlDocument();

XMLIns = XMLDoc.CreateProcessingInstruction("xml","version='1.0'");
XMLNodeIns = XMLDoc.AppendChild(XMLIns);

XMLElemDNList = XMLDoc.CreateElement("DeliveryList");
XMLNodeDN = XMLNodeIns.AppendChild(XMLElemDNList);

XMLElem = XMLDoc.CreateElement("DeliveryNote");
XMLNodeDN.AppendChild(XMLElem);

XMLElem = XMLDoc.CreateElement("DocNo");
XMLNodeDocNo = XMLNodeDN.AppendChild(XMLElem);
XMLNodeDocNo.InnerText = DocNo;

XMLElem = XMLDoc.CreateElement("Signature");
XMLNodeSignature = XMLNodeDN.AppendChild(XMLElem);
XMLNodeSignature.InnerText = Signature.ToString();

DataTable table = dsDN.Tables["ScmOrder"];
DataRow[] foundRows = table.Select("DocNo = '" + DocNo + "'");

foreach (DataRow row in foundRows)
{
string ItemNo = (string) row["ItemNo"];
string Qty = (string) row["Qty"];
bool ItemAdded = (bool) row["ItemAdded"];
if (ItemAdded == true)
{
XMLElem = XMLDoc.CreateElement("ItemPos");
XMLNodeItem = XMLNodeDN.AppendChild(XMLElem);

XMLElem = XMLDoc.CreateElement("ItemNo");
XMLNodeItemNo = XMLNodeItem.AppendChild(XMLElem);
XMLNodeItemNo.InnerText = ItemNo;

XMLElem = XMLDoc.CreateElement("Qty");
XMLNodeQty = XMLNodeItem.AppendChild(XMLElem);
XMLNodeQty.InnerText = Qty.Replace(",",".");
}
row.Delete();
}

FileName = FilePath + FileName + FileExt;

XmlTextWriter xwriter = new XmlTextWriter(FileName,System.Text.Encoding.UTF8);

XMLDoc.WriteTo(xwriter);

xwriter.Close();

return true;
}


Der Fehler tritt in der Zeile 16 "XMLNodeDN = XMLNodeIns.AppendChild(XMLElemDNList);" auf.

Hat jemand eine Idee was ich da machen kann - ist sicherlich nur eine Kleinigkeit - aber mich bringt dies zum Verzweifeln.
 
Zurück