1

I want to add the ability to add an image to the file. I have the functionality to convert image to string and back to image, which works fine.

XML File Data

<MovieData>
    <Movie>
        <Name>Death Race</Name>
        <Type>Action</Type>
        <Type>Adventure</Type>
        <Rating>R</Rating>
        <Disk>Blu-Ray</Disk>
        <Owner>N/A</Owner>
        <Location>N/A</Location>
        <SeriesType>Movie Series</SeriesType>
        <LengthHr>1</LengthHr>
        <LengthMin>51</LengthMin>
        <Time>10 : 44 : 23 PM</Time>
        <Date>10/13/2013</Date>
      </Movie>
      <Movie>
        <Name>Death Race 2</Name>
        <Type>Action</Type>
        <Type>Adventure</Type>
        <Rating>R</Rating>
        <Disk>Combo</Disk>
        <Owner>N/A</Owner>
        <Location>N/A</Location>
        <SeriesType>Movie Series</SeriesType>
        <LengthHr>1</LengthHr>
        <LengthMin>41</LengthMin>
        <Time>9 : 52 : 34 PM</Time>
        <Date>10/9/2013</Date>
      </Movie>
</MovieData>

What I want it to look like:

<MovieData>
      <Movie>
        <Name>Death Race</Name>
        <Type>Action</Type>
        <Type>Adventure</Type>
        <Rating>R</Rating>
        <Disk>Blu-Ray</Disk>
        <Owner>N/A</Owner>
        <Location>N/A</Location>
        <SeriesType>Movie Series</SeriesType>
        <LengthHr>1</LengthHr>
        <LengthMin>51</LengthMin>
        <Image>string</Image>//Needs to be here.If it is after date that is fine too.
        <Time>10 : 44 : 23 PM</Time>
        <Date>10/13/2013</Date>
      </Movie>
      <Movie>
        <Name>Death Race 2</Name>
        <Type>Action</Type>
        <Type>Adventure</Type>
        <Rating>R</Rating>
        <Disk>Combo</Disk>
        <Owner>N/A</Owner>
        <Location>N/A</Location>
        <SeriesType>Movie Series</SeriesType>
        <LengthHr>1</LengthHr>
        <LengthMin>41</LengthMin>
        <Image>string</Image>
        <Time>9 : 52 : 34 PM</Time>
        <Date>10/9/2013</Date>
      </Movie>
</MovieData>

Code:

try
{
    string name = movieSaveImageNameTB.Text;
    string date = movieSaveImageDateTB.Text;
    string time = movieSaveImageTimeTB.Text;
    string hr = movieSaveImageHrTB.Text;
    string min = movieSaveImageMinTB.Text;
    XmlDocument doc = new XmlDocument();
    doc.Load(movieListXML);
    XmlNode node = doc.SelectSingleNode("/MovieData");
    foreach (XmlNode movie in node.SelectNodes("Movie"))
    {
        if (movie != null)
        {
            if ((movie["Name"].InnerText == name) && (movie["Date"].InnerText == date) && (movie["Time"].InnerText == time) && 
                        (movie["LengthHr"].InnerText == hr) && (movie["LengthMin"].InnerText == min))
            {
                // This works but doesn't give the results i want.
                XmlNode n = doc.CreateNode(movie["Name"].NodeType, "Image", movie.NamespaceURI);
                movie.InsertAfter(n, movie.LastChild);
                doc.Save(movieListXML);
            }
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

I updated it with the correct XML file format i am using. Sorry about that

I tried this.

XmlNodeList nodeList = movie.ChildNodes;
foreach (XmlNode nl in nodeList)
{
    if (nl.Name == "LengthMin")
    {
        XmlElement xNewChild = doc.CreateElement("Image");
        xNewChild.InnerText = "string";
        doc.DocumentElement.InsertAfter(xNewChild, nl);
    }
}

It still shows the error saying that it is not a child of that node...

2
  • Try to write code by urself ... see the updated answer Commented Oct 21, 2013 at 7:13
  • As you said Code has been changed now u can accept the answer !! Commented Oct 21, 2013 at 8:44

1 Answer 1

1

try this

  XmlDocument xDoc = new XmlDocument();
        xDoc.Load("E:\\test.xml");
        XmlNodeList xE = xDoc.SelectNodes("//MovieData/Movie/LengthMin");
        Dictionary<string, string> dicMovieData = null;
        if (xE != null)
        {
            for (int iVal = 0; iVal < xE.Count; iVal++)
            {
                if (xE[iVal] is XmlNode)
                {
                    XElement xElement = XElement.Parse("<Temp>" + xE[iVal].ParentNode.InnerXml + "</Temp>");
                    if (xElement != null)
                    {
                        dicMovieData = new Dictionary<string, string>();
                        foreach (XElement xMovieData in xElement.Descendants())
                        {
                            if (!dicMovieData.ContainsKey(xMovieData.Name.LocalName))
                                dicMovieData.Add(xMovieData.Name.LocalName, xMovieData.Value);
                        }
                        string sName = "Death Race";
                        string sDate = "10/13/2013";
                        string sTime = "10:44:23 PM";
                        string sLenghHR = "1";
                        string sLengthMin = "51";
                        if (dicMovieData != null && dicMovieData.Count > 0)
                        {
                            if (string.Compare(dicMovieData["Name"], sName, true) == 0
                                && string.Compare(dicMovieData["Date"], sDate, true) == 0
                                && string.Compare(dicMovieData["Time"], sTime, true) == 0
                                && string.Compare(dicMovieData["LengthHr"], sLenghHR, true) == 0
                                && string.Compare(dicMovieData["LengthMin"], sLengthMin, true) == 0)
                            {
                                XmlElement xNewChild = xDoc.CreateElement("Image");
                                xNewChild.InnerText = "string";
                                XmlNode commonParent = xE[iVal].ParentNode;
                                commonParent.InsertAfter(xNewChild, xE[iVal]);
                            }
                        }
                    }
                }
            }
            xDoc.Save("D:\\test.xml");
        }

Old XMl

<MovieData>
<Movie>
    <Name>Death Race</Name>
    <Type>Action</Type>
    <Type>Adventure</Type>
    <Rating>R</Rating>
    <Disk>Blu-Ray</Disk>
    <Owner>N/A</Owner>
    <Location>N/A</Location>
    <SeriesType>Movie Series</SeriesType>
    <LengthHr>1</LengthHr>
    <LengthMin>51</LengthMin>
    <Time>10 : 44 : 23 PM</Time>
    <Date>10/13/2013</Date>
  </Movie>
  <Movie>
    <Name>Death Race 2</Name>
    <Type>Action</Type>
    <Type>Adventure</Type>
    <Rating>R</Rating>
    <Disk>Combo</Disk>
    <Owner>N/A</Owner>
    <Location>N/A</Location>
    <SeriesType>Movie Series</SeriesType>
    <LengthHr>1</LengthHr>
    <LengthMin>41</LengthMin>
    <Time>9 : 52 : 34 PM</Time>
    <Date>10/9/2013</Date>
  </Movie>
 </MovieData>

New Xml

<MovieData>
 <Movie>
<Name>Death Race</Name>
<Type>Action</Type>
<Type>Adventure</Type>
<Rating>R</Rating>
<Disk>Blu-Ray</Disk>
<Owner>N/A</Owner>
<Location>N/A</Location>
<SeriesType>Movie Series</SeriesType>
<LengthHr>1</LengthHr>
<LengthMin>51</LengthMin>
<Image>string</Image>       /// Node Added
<Time>10 : 44 : 23 PM</Time>
<Date>10/13/2013</Date>
</Movie>
 <Movie>
<Name>Death Race 2</Name>
<Type>Action</Type>
<Type>Adventure</Type>
<Rating>R</Rating>
<Disk>Combo</Disk>
<Owner>N/A</Owner>
<Location>N/A</Location>
<SeriesType>Movie Series</SeriesType>
<LengthHr>1</LengthHr>
<LengthMin>41</LengthMin>
<Time>9 : 52 : 34 PM</Time>
<Date>10/9/2013</Date>
 </Movie>
 </MovieData>
Sign up to request clarification or add additional context in comments.

17 Comments

I'm using XmlDocument to do all the searching. That method would make it so i have to change all my code and I'm more familiar with XmlDocument. Please provide a fix to the current code i have. Thanks for trying. I just want a fix for what i am using now.
you said XElement instead of that you have to say node
but try to use XDocument...it will be more easier to use
You are right i did say XElement instead of node i am sorry i will change title. I use XElements to add new objects to xml but i edit with LinQ. Sorry about that.
SEE THE ANSWER I HAVE UPDATED
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.