By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

Trying to dynamically create child elements in an XML document

P: n/a
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(taxingAuthority ListEl);
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(taxingAuthority List1El);

return xmlDoc;
}
Apr 14 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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(taxingAuthority ListEl);
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" wrote:
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(taxingAuthority ListEl);
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(taxingAuthority List1El);

return xmlDoc;
}

Apr 15 '06 #2

P: n/a
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" <Sa*****************@discussions.microsoft.com>
wrote in message news:0C**********************************@microsof t.com...
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(taxingAuthority ListEl);
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" wrote:
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(taxingAuthority ListEl);
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(taxingAuthority List1El);

return xmlDoc;
}

Apr 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.