Trying to dynamically create child elements in an XML document

T

Thirsty Traveler

How would I dynamcially create an XML document of the following form:

<ReceiveTSSCallBack xmlns="http://LandAm.EAI.Mainframe.TSR">
<TSSCallBack
xmlns="http://LandAm.EAI.Mainframe.TSR.Schemas.TSSCallback">
<orderNo xmlns="">string</orderNo>
<customerId xmlns="">string</customerId>
<taxingAuthorityList xmlns="">
<string>string</string>
<string>string</string>
</taxingAuthorityList>
</TSSCallBack>
</ReceiveTSSCallBack>

I am at a loss on how to create child nodes under taxingAuthorityList. My
attempt, so far, looks like this:

public XmlDocument CreateCallbackXml(StringBuilder orderNo, StringBuilder
customerId, StringBuilder[] taxingAuthorityList)
{
XmlDocument xmlDoc = new XmlDocument();
XmlText xmlText;
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0",
"utf-8", null);
XmlElement root = xmlDoc.CreateElement("ReceiveTSSCallBack");
root.SetAttribute("xmlns", "http://LandAm.EAI.Mainframe.TSR");
xmlDoc.AppendChild(root);

XmlElement orderNoEl = xmlDoc.CreateElement("orderNo");
xmlText = xmlDoc.CreateTextNode(orderNo.ToString());
orderNoEl.AppendChild(xmlText);
xmlDoc.DocumentElement.PrependChild(orderNoEl);

XmlElement customerIdEl = xmlDoc.CreateElement("customerId");
xmlText = xmlDoc.CreateTextNode(customerId.ToString());
customerIdEl.AppendChild(xmlText);
xmlDoc.DocumentElement.AppendChild(customerIdEl);

XmlElement taxingAuthorityListEl =
xmlDoc.CreateElement("taxingAuthorityList");
taxingAuthorityListEl.SetAttribute("xmlns", "");
xmlDoc.DocumentElement.AppendChild(taxingAuthorityListEl);
xmlDoc.CreateNode(XmlNodeType.Element, "taxingAuthorityList", "");

// STRUGGLING HERE... NOT SURE OF HOW TO CREATE CHILD ELEMENTS FOR
taxingAuthorityList
//XmlElement taxingAuthorityList1El = xmlDoc.CreateElement("string");
//xmlText = xmlDoc.CreateTextNode(errorMessage.ToString());
//errorMessageEl.AppendChild(xmlText);
//xmlDoc.DocumentElement. .InsertAfter(taxingAuthorityList1El,
taxingAuthorityListEl);
//xmlDoc.DocumentElement.AppendChild(taxingAuthorityList1El);

return xmlDoc;
}
 
G

Guest

From your example posted have a look at a modified version:
----------------------------------------------------------------------------------------
public XmlDocument CreateCallbackXml()
{
StringBuilder orderNo = new StringBuilder();
orderNo.Append("3");
StringBuilder customerId = new StringBuilder();
customerId.Append("123456");

XmlDocument xmlDoc = new XmlDocument();
XmlText xmlText;
XmlDeclaration xmlDeclaration =
xmlDoc.CreateXmlDeclaration("1.0","utf-8", null);
XmlElement root = xmlDoc.CreateElement("ReceiveTSSCallBack");
root.SetAttribute("xmlns", "http://LandAm.EAI.Mainframe.TSR");
xmlDoc.AppendChild(root);

XmlElement orderNoEl = xmlDoc.CreateElement("orderNo");
xmlText = xmlDoc.CreateTextNode(orderNo.ToString());
orderNoEl.AppendChild(xmlText);
xmlDoc.DocumentElement.PrependChild(orderNoEl);

XmlElement customerIdEl = xmlDoc.CreateElement("customerId");
xmlText = xmlDoc.CreateTextNode(customerId.ToString());
customerIdEl.AppendChild(xmlText);
xmlDoc.DocumentElement.AppendChild(customerIdEl);

XmlElement taxingAuthorityListEl =
xmlDoc.CreateElement("taxingAuthorityList");
taxingAuthorityListEl.SetAttribute("xmlns", "");
xmlDoc.DocumentElement.AppendChild(taxingAuthorityListEl);
xmlDoc.CreateNode(XmlNodeType.Element, "taxingAuthorityList", "");

XmlElement child1 = xmlDoc.CreateElement("ChildOne");
taxingAuthorityListEl.AppendChild(child1);
child1.InnerText = "Child one here";
XmlAttribute att1 = xmlDoc.CreateAttribute("Child1Att");
att1.Value = "Child one attribute";
child1.Attributes.Append(att1);

return xmlDoc;
}
----------------------------------------------------------------------------------------

BTW: Why are you passing these StringBuilder objects? It seems to me you are
using StringBuilder object where you really should be using String. For
instance is the customerId a sequence of text that will be modified many
times? If not then it would be better to use normal Strings as opposed to
StringBuilder.

Sayed Ibrahim Hashimi
www.sedodream.com

Thirsty Traveler said:
How would I dynamcially create an XML document of the following form:

<ReceiveTSSCallBack xmlns="http://LandAm.EAI.Mainframe.TSR">
<TSSCallBack
xmlns="http://LandAm.EAI.Mainframe.TSR.Schemas.TSSCallback">
<orderNo xmlns="">string</orderNo>
<customerId xmlns="">string</customerId>
<taxingAuthorityList xmlns="">
<string>string</string>
<string>string</string>
</taxingAuthorityList>
</TSSCallBack>
</ReceiveTSSCallBack>

I am at a loss on how to create child nodes under taxingAuthorityList. My
attempt, so far, looks like this:

public XmlDocument CreateCallbackXml(StringBuilder orderNo, StringBuilder
customerId, StringBuilder[] taxingAuthorityList)
{
XmlDocument xmlDoc = new XmlDocument();
XmlText xmlText;
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0",
"utf-8", null);
XmlElement root = xmlDoc.CreateElement("ReceiveTSSCallBack");
root.SetAttribute("xmlns", "http://LandAm.EAI.Mainframe.TSR");
xmlDoc.AppendChild(root);

XmlElement orderNoEl = xmlDoc.CreateElement("orderNo");
xmlText = xmlDoc.CreateTextNode(orderNo.ToString());
orderNoEl.AppendChild(xmlText);
xmlDoc.DocumentElement.PrependChild(orderNoEl);

XmlElement customerIdEl = xmlDoc.CreateElement("customerId");
xmlText = xmlDoc.CreateTextNode(customerId.ToString());
customerIdEl.AppendChild(xmlText);
xmlDoc.DocumentElement.AppendChild(customerIdEl);

XmlElement taxingAuthorityListEl =
xmlDoc.CreateElement("taxingAuthorityList");
taxingAuthorityListEl.SetAttribute("xmlns", "");
xmlDoc.DocumentElement.AppendChild(taxingAuthorityListEl);
xmlDoc.CreateNode(XmlNodeType.Element, "taxingAuthorityList", "");

// STRUGGLING HERE... NOT SURE OF HOW TO CREATE CHILD ELEMENTS FOR
taxingAuthorityList
//XmlElement taxingAuthorityList1El = xmlDoc.CreateElement("string");
//xmlText = xmlDoc.CreateTextNode(errorMessage.ToString());
//errorMessageEl.AppendChild(xmlText);
//xmlDoc.DocumentElement. .InsertAfter(taxingAuthorityList1El,
taxingAuthorityListEl);
//xmlDoc.DocumentElement.AppendChild(taxingAuthorityList1El);

return xmlDoc;
}
 
T

Thirsty Traveler

The parameters are coming from the mainframe where the interface code
generator creates them as stringbuilder objects.

My biggest problem was not with StingBuilder but, rather, not knowing how to
create the taxingAutorityListEl.

Anyway, I appreciate your help on this and will give it a try.

Sayed Ibrahim Hashimi said:
From your example posted have a look at a modified version:
----------------------------------------------------------------------------------------
public XmlDocument CreateCallbackXml()
{
StringBuilder orderNo = new StringBuilder();
orderNo.Append("3");
StringBuilder customerId = new StringBuilder();
customerId.Append("123456");

XmlDocument xmlDoc = new XmlDocument();
XmlText xmlText;
XmlDeclaration xmlDeclaration =
xmlDoc.CreateXmlDeclaration("1.0","utf-8", null);
XmlElement root = xmlDoc.CreateElement("ReceiveTSSCallBack");
root.SetAttribute("xmlns", "http://LandAm.EAI.Mainframe.TSR");
xmlDoc.AppendChild(root);

XmlElement orderNoEl = xmlDoc.CreateElement("orderNo");
xmlText = xmlDoc.CreateTextNode(orderNo.ToString());
orderNoEl.AppendChild(xmlText);
xmlDoc.DocumentElement.PrependChild(orderNoEl);

XmlElement customerIdEl = xmlDoc.CreateElement("customerId");
xmlText = xmlDoc.CreateTextNode(customerId.ToString());
customerIdEl.AppendChild(xmlText);
xmlDoc.DocumentElement.AppendChild(customerIdEl);

XmlElement taxingAuthorityListEl =
xmlDoc.CreateElement("taxingAuthorityList");
taxingAuthorityListEl.SetAttribute("xmlns", "");
xmlDoc.DocumentElement.AppendChild(taxingAuthorityListEl);
xmlDoc.CreateNode(XmlNodeType.Element, "taxingAuthorityList", "");

XmlElement child1 = xmlDoc.CreateElement("ChildOne");
taxingAuthorityListEl.AppendChild(child1);
child1.InnerText = "Child one here";
XmlAttribute att1 = xmlDoc.CreateAttribute("Child1Att");
att1.Value = "Child one attribute";
child1.Attributes.Append(att1);

return xmlDoc;
}
----------------------------------------------------------------------------------------

BTW: Why are you passing these StringBuilder objects? It seems to me you
are
using StringBuilder object where you really should be using String. For
instance is the customerId a sequence of text that will be modified many
times? If not then it would be better to use normal Strings as opposed to
StringBuilder.

Sayed Ibrahim Hashimi
www.sedodream.com

Thirsty Traveler said:
How would I dynamcially create an XML document of the following form:

<ReceiveTSSCallBack xmlns="http://LandAm.EAI.Mainframe.TSR">
<TSSCallBack
xmlns="http://LandAm.EAI.Mainframe.TSR.Schemas.TSSCallback">
<orderNo xmlns="">string</orderNo>
<customerId xmlns="">string</customerId>
<taxingAuthorityList xmlns="">
<string>string</string>
<string>string</string>
</taxingAuthorityList>
</TSSCallBack>
</ReceiveTSSCallBack>

I am at a loss on how to create child nodes under taxingAuthorityList. My
attempt, so far, looks like this:

public XmlDocument CreateCallbackXml(StringBuilder orderNo, StringBuilder
customerId, StringBuilder[] taxingAuthorityList)
{
XmlDocument xmlDoc = new XmlDocument();
XmlText xmlText;
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0",
"utf-8", null);
XmlElement root = xmlDoc.CreateElement("ReceiveTSSCallBack");
root.SetAttribute("xmlns", "http://LandAm.EAI.Mainframe.TSR");
xmlDoc.AppendChild(root);

XmlElement orderNoEl = xmlDoc.CreateElement("orderNo");
xmlText = xmlDoc.CreateTextNode(orderNo.ToString());
orderNoEl.AppendChild(xmlText);
xmlDoc.DocumentElement.PrependChild(orderNoEl);

XmlElement customerIdEl = xmlDoc.CreateElement("customerId");
xmlText = xmlDoc.CreateTextNode(customerId.ToString());
customerIdEl.AppendChild(xmlText);
xmlDoc.DocumentElement.AppendChild(customerIdEl);

XmlElement taxingAuthorityListEl =
xmlDoc.CreateElement("taxingAuthorityList");
taxingAuthorityListEl.SetAttribute("xmlns", "");
xmlDoc.DocumentElement.AppendChild(taxingAuthorityListEl);
xmlDoc.CreateNode(XmlNodeType.Element, "taxingAuthorityList", "");

// STRUGGLING HERE... NOT SURE OF HOW TO CREATE CHILD ELEMENTS FOR
taxingAuthorityList
//XmlElement taxingAuthorityList1El = xmlDoc.CreateElement("string");
//xmlText = xmlDoc.CreateTextNode(errorMessage.ToString());
//errorMessageEl.AppendChild(xmlText);
//xmlDoc.DocumentElement. .InsertAfter(taxingAuthorityList1El,
taxingAuthorityListEl);
//xmlDoc.DocumentElement.AppendChild(taxingAuthorityList1El);

return xmlDoc;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Dictionary To Xml 1

Top