Note that there are some explanatory texts on larger screens.

plurals
  1. POUndesired results appending new child nodes using XmlDocument and XmlDocumentFragment
    primarykey
    data
    text
    <p>What I planned was to append <code>&lt;image&gt;@matchedFilePathToAnImageHere&lt;/image&gt;</code> only to those <code>&lt;item&gt;&lt;/item&gt;</code> nodes whose values between <code>&lt;name&gt;&lt;/name&gt;</code> tags, when converted to lower case, replaced spaces with underscores and etc, would match actual image file names in a separate folder.</p> <p>The code properly matches about 95% of images to the items, <strong>but</strong> ends up appending every matched image file name with <code>&lt;image&gt;&lt;/image&gt;</code> to the very first <code>&lt;item&gt;&lt;/item&gt;</code>.</p> <p><em>How would I append every <code>&lt;image&gt;&lt;/image&gt;</code> to their appropriate <code>&lt;item&gt;&lt;/item&gt;</code>?</em> Every item needs only one image.</p> <p><strong>Images folder</strong>:</p> <p>name1.jpg </p> <p>name_2.jpg </p> <p>name3.jpg </p> <p>... </p> <p>name 998.jpg</p> <p><strong>XML(before parsing)</strong>:</p> <pre><code>&lt;items&gt; &lt;item&gt; &lt;name&gt;Name1&lt;/name&gt; &lt;price&gt;Price1&lt;/price&gt; &lt;description&gt;Description1&lt;/description&gt; &lt;/item&gt; &lt;item&gt; &lt;name&gt;Name2&lt;/name&gt; &lt;price&gt;Price2&lt;/price&gt; &lt;description&gt;Description2&lt;/description&gt; &lt;/item&gt; &lt;item&gt; &lt;name&gt;Name3&lt;/name&gt; &lt;price&gt;Price3&lt;/price&gt; &lt;description&gt;Description3&lt;/description&gt; &lt;/item&gt; &lt;/items&gt; </code></pre> <p><strong>XML(desired result after parsing)</strong>:</p> <pre><code>&lt;items&gt; &lt;item&gt; &lt;name&gt;name1&lt;/name&gt; &lt;price&gt;Price1&lt;/price&gt; &lt;description&gt;Description1&lt;/description&gt; &lt;image&gt;C:\path\to\name1.jpg&lt;/image&gt; &lt;/item&gt; &lt;item&gt; &lt;name&gt;Name2&lt;/name&gt; &lt;price&gt;Price2&lt;/price&gt; &lt;description&gt;Description2&lt;/description&gt; &lt;!-- no image file name matched `name2`(command line notice), so skip appending image tags here BUT I add the image tag here later by hand, because I find out that there's an image `name_2.jpg` --&gt; &lt;/item&gt; &lt;item&gt; &lt;name&gt;Name3&lt;/name&gt; &lt;price&gt;Price3&lt;/price&gt; &lt;description&gt;Description3&lt;/description&gt; &lt;image&gt;C:\path\to\name3.jpg&lt;/image&gt; &lt;/item&gt; &lt;/items&gt; </code></pre> <p><strong>Code</strong>:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; using System.IO; namespace myXmlParser { class Program { static void Main(string[] args) { // load the xml XmlDocument doc = new XmlDocument(); doc.Load(@"C:\items.xml"); // retrieve the values between &lt;name&gt;&lt;/name&gt; for the every item element XmlNodeList nodes = doc.SelectNodes("//item/name/text()"); // convert every extracted name value to lower case // replace spaces with underscores // remove the ' symbols // to have higher chance of matching images' file names // ",_the" and "_,a" replaces don't seem to work( oh well ) for (int i = 0; i &lt; nodes.Count; i++) { // do the magic! string searchKeyword = nodes.Item(i).Value.Replace(" ", "_").Replace("'","").Replace(",_the",(string)"").Replace(",_a","").ToLower(); //Console.WriteLine(searchKeyword); // Now find me any images whose filenames match the searchKeyword minus the extensions string[] filePaths = Directory.GetFiles(@"C:\images", searchKeyword + "*", SearchOption.TopDirectoryOnly); // if something was found/matched then append &lt;image&gt;@pathToImageHere&lt;/image&gt; to the current // item node, otherwise log any item nodes that didn't have a match to an image // ! Doesn't APPEND properly ! if (filePaths.Length &gt; 0) { XmlDocumentFragment frag = doc.CreateDocumentFragment(); frag.InnerXml = @"&lt;image&gt;" + filePaths[0] + @"&lt;/image&gt;"; doc.DocumentElement.FirstChild.AppendChild(frag); } else { Console.WriteLine("NO IMAGE WAS FOUND!!! {0}", searchKeyword); } //Console.WriteLine(filePaths[j]); //foreach (string filePath in filePaths) //{ //blah //} } // now save the new parsed xml somewhere doc.Save("items_with_images.xml"); Console.ReadKey(); }// main }// class }// namespace </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload