Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8346593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:04:23+00:00 2026-06-09T07:04:23+00:00

What I planned was to append <image>@matchedFilePathToAnImageHere</image> only to those <item></item> nodes whose values

  • 0

What I planned was to append <image>@matchedFilePathToAnImageHere</image> only to those <item></item> nodes whose values between <name></name> tags, when converted to lower case, replaced spaces with underscores and etc, would match actual image file names in a separate folder.

The code properly matches about 95% of images to the items, but ends up appending every matched image file name with <image></image> to the very first <item></item>.

How would I append every <image></image> to their appropriate <item></item>? Every item needs only one image.

Images folder:

name1.jpg

name_2.jpg

name3.jpg

…

name 998.jpg

XML(before parsing):

<items>
 <item>
  <name>Name1</name>
  <price>Price1</price>
  <description>Description1</description>
 </item>
 <item>
  <name>Name2</name>
  <price>Price2</price>
  <description>Description2</description>
 </item>
 <item>
  <name>Name3</name>
  <price>Price3</price>
  <description>Description3</description>
 </item>
</items>

XML(desired result after parsing):

<items>
 <item>
  <name>name1</name>
  <price>Price1</price>
  <description>Description1</description>
  <image>C:\path\to\name1.jpg</image>
 </item>
 <item>
  <name>Name2</name>
  <price>Price2</price>
  <description>Description2</description>
      <!-- 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` -->
 </item>
     <item>
  <name>Name3</name>
  <price>Price3</price>
  <description>Description3</description>
  <image>C:\path\to\name3.jpg</image>
 </item>
</items>

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 <name></name> 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 < 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 <image>@pathToImageHere</image> 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 > 0)
                {
                    XmlDocumentFragment frag = doc.CreateDocumentFragment();
                    frag.InnerXml = @"<image>" + filePaths[0] + @"</image>";
                    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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:04:24+00:00Added an answer on June 9, 2026 at 7:04 am
    doc.DocumentElement.FirstChild.AppendChild(frag);
    

    Why do you append to the first child of the document. Aren’t you expected to append to the current node?

     nodes.Item(i).ParentNode.ParentNode.AppendChild(frag);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I planned to make an iPhone app which store to-do lists like thing. What
I planned to use slider in my magento homepage. The slider should cover entirely
I am writing a text editor and had planned on using the richtextbox control
In short, when you have planned or have thought about an idea to create
My company is a Software development company. We planned to use TFS 2010 for
Any ideas about escape analysis in dalvik? Or when and if it's planned to
I have a fairly large C++ application (on Windows, no other platforms planned), which
I'm using SQL Express 2008 edition. I've planned my database tables/relationships on a piece
We have a Spring + Ibatis based J2EE app. I planned to wrap around
My place of work currently uses CVS. A git migration is planned but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.