I am using c# to create a xml file, however I got some problem. I would like to have both of parent and child nodes with a same attribute. But only one of those nodes has the attribute, even though I appended both of those.
what I expected:
<request>
<transaction transactionId:"123">
<transactionDetail transactionId:"123"></transactionDetail>
</transaction>
</request>
what I got:
<request>
<transaction>
<transactionDetail transactionId:"123"></transactionDetail>
</transaction>
</request>
or
<request>
<transaction transactionId:"123">
<transactionDetail></transactionDetail>
</transaction>
</request>
This is depends on the order that I write code (The node I append later has attribute). Could you please help me out to find what's causing this problem?
Also, I just wonder:
Does the order of appending (both of .AppendChild() & .Append() ) matter?
Can't I reuse attributes which are already appended in other nodes?
The following is the function to create xml file:
public ActionResult createXMLFile() {
XmlDocument xmlFile = new XmlDocument();
XmlNode request = xmlFile.CreateElement("request");
XmlNode transaction= xmlFile.CreateElement("transaction");
XmlNode transactionDetail= xmlFile.CreateElement("transactionDetail");
XmlAttribute transactionId= xmlFile.CreateAttribute("transactionId");
transactionId.Value = "123";
transaction.Attributes.Append(transactionId);
transactionDetail.Attributes.Append(transactionId);
xmlFile.AppendChild(request);
request.AppendChild(transaction);
transaction.AppendChild(transactionDetail);
string path ="somepath";
xmlFile.Save(path);
}
Thank you for reading my question. :)