C# XML Formatting mistake in code-generated XML output

hhhgame

New Member
So I have a file that already exists, and I read this file and add some nodes to it. Due to the nature of the processing required, I have my code in a loop, so for each row in a given data table, I open the existing file, write my new nodes, and close the file.The first iteration inserts the node group perfectly. Every iteration thereafter is problematic and somehow adds to the first grouping instead of creating its own grouping.This is what each grouping should look like and this is how it generates the first one:\[code\] <item identifier="ITEM-F2D7FEDF240B4DCCBF346DBE2C47AC89" identifierref="RES-770DCE40C5BA4E97B1E3B3DB49BBBD4F" isvisible="true" parameters=""> <title>Title1</title> <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2"> </adlcp:datafromlms> </item>\[/code\]Once the whole thing is processed though, it ends up looking like this:\[code\] <item identifier="ITEM-F2D7FEDF240B4DCCBF346DBE2C47AC89" identifierref="RES-770DCE40C5BA4E97B1E3B3DB49BBBD4F" isvisible="true" parameters=""> <title>Title1</title> <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2"> </adlcp:datafromlms> <title>Title2</title> <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2"> </adlcp:datafromlms> <title>Title3</title> <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2"> </adlcp:datafromlms> <title>Title4</title> <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2"> </adlcp:datafromlms> <title>Title5</title> <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2"> </adlcp:datafromlms> </item> <item identifier="ITEM-4D80AFFE59D04E2188F39908B9325961" identifierref="RES-A9CFDC9208714DAF9EA351D4656A7EBC" isvisible="true" parameters="" /> <item identifier="ITEM-F4EDB38AD0D74CC38722E6D1A8D67E24" identifierref="RES-E2F92D4C5165482386421944053EE933" isvisible="true" parameters="" /> <item identifier="ITEM-BF1C7474919B4B22BC300F98034ABDD1" identifierref="RES-8A0ED1C94CA44A71A07A8A4A5DA2A528" isvisible="true" parameters="" /> <item identifier="ITEM-156731B2ABB14AB29135CBF5D8CBCFF3" identifierref="RES-D452D539C49A4D65BC3A8AC6B16DE718" isvisible="true" parameters="" />\[/code\]So basically it ends up tacking the bulk of the new data into the existing node (where I don't want it) and creating the new item node at the bottom of the organization group (which is where it should be). I need the title and adlcp entries to be attached under each of the new item nodes.Here's the code I'm using. Remember that this code gets executed multiple times upon the same file, once for each set of entries. There's an additional node created by the code that goes into another place called resources, but that part works fine so I didn't include it in the XML excerpts above.\[code\] string strItem = Guid.NewGuid().ToString("N").ToUpper(); // GUID for random unique value. string strRes = Guid.NewGuid().ToString("N").ToUpper(); // GUID for random unique value. XmlDocument docXMLFile = new XmlDocument(); docXMLFile.Load(resultPath + "imsmanifest.xml"); // Load file #region Item Element Creation XmlNode xItem = docXMLFile.CreateNode(XmlNodeType.Element, "item", docXMLFile.DocumentElement.NamespaceURI); XmlAttribute xIdentifier = docXMLFile.CreateAttribute("identifier"); XmlAttribute xIdentifierRef = docXMLFile.CreateAttribute("identifierref"); XmlAttribute xIsVisible = docXMLFile.CreateAttribute("isvisible"); XmlAttribute xParameters = docXMLFile.CreateAttribute("parameters"); xIdentifier.Valuehttp://stackoverflow.com/questions/10754824/= "http://stackoverflow.com/questions/10754824/ITEM-" + strItem; xIdentifierRef.Valuehttp://stackoverflow.com/questions/10754824/= "http://stackoverflow.com/questions/10754824/RES-" + strRes; xIsVisible.Valuehttp://stackoverflow.com/questions/10754824/= "http://stackoverflow.com/questions/10754824/true"; xParameters.Valuehttp://stackoverflow.com/questions/10754824/= ""; xItem.Attributes.Append(xIdentifier); xItem.Attributes.Append(xIdentifierRef); xItem.Attributes.Append(xIsVisible); xItem.Attributes.Append(xParameters); // NOTE - the docXMLFile.DocumentElement.NamespaceURI GETS RID OF XMLNS="" WHICH IS BULLSHIT. XmlNode xTitle = docXMLFile.CreateNode(XmlNodeType.Element, "title", docXMLFile.DocumentElement.NamespaceURI); if ((dataRow["product_name"].ToString() + " - " + dataRow["topic_name"].ToString()).Count() > 255) xTitle.InnerText = (dataRow["product_name"].ToString() + " - " + dataRow["topic_name"].ToString()).Substring(0, 255); else xTitle.InnerText = dataRow["product_name"].ToString() + " - " + dataRow["topic_name"].ToString(); XmlNode xADLCPDataFromLMS = docXMLFile.CreateNode(XmlNodeType.Element, "adlcp:datafromlms", docXMLFile.DocumentElement.NamespaceURI); xADLCPDataFromLMS.InnerText = dataRow["datafromlms"].ToString(); // This is where the new stuff gets inserted. docXMLFile.GetElementsByTagName("organization")[0].InsertAfter(xItem, docXMLFile.GetElementsByTagName("organization")[0].LastChild); docXMLFile.GetElementsByTagName("item")[0].InsertAfter(xTitle, docXMLFile.GetElementsByTagName("item")[0].LastChild); docXMLFile.GetElementsByTagName("item")[0].InsertAfter(xADLCPDataFromLMS, docXMLFile.GetElementsByTagName("item")[0].LastChild); #endregion #region Resource Element Creation XmlNode xResource = docXMLFile.CreateNode(XmlNodeType.Element, "resource", docXMLFile.DocumentElement.NamespaceURI); XmlAttribute xRefIdentifier = docXMLFile.CreateAttribute("identifier"); XmlAttribute xRefADLCP = docXMLFile.CreateAttribute("adlcp:scormtype"); XmlAttribute xRefHREF = http://stackoverflow.com/questions/10754824/docXMLFile.CreateAttribute("href"); XmlAttribute xRefType = docXMLFile.CreateAttribute("type"); xRefIdentifier.Valuehttp://stackoverflow.com/questions/10754824/= "http://stackoverflow.com/questions/10754824/RES-" + strRes; xRefADLCP.Valuehttp://stackoverflow.com/questions/10754824/= "sco"; xRefHREF.Value = http://stackoverflow.com/questions/10754824/dataRow["launch_url"].ToString().ToLower(); xRefType.Valuehttp://stackoverflow.com/questions/10754824/= "webcontent"; xResource.Attributes.Append(xRefIdentifier); xResource.Attributes.Append(xRefADLCP); xResource.Attributes.Append(xRefHREF); xResource.Attributes.Append(xRefType); docXMLFile.GetElementsByTagName("resources")[0].InsertAfter(xResource, docXMLFile.GetElementsByTagName("resources")[0].LastChild); #endregion docXMLFile.Save(resultPath + "imsmanifest.xml"); //save\[/code\]
 
Back
Top