1

我想添加将图像添加到文件的功能。我具有将图像转换为字符串并返回图像的功能,效果很好。

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>

我希望它看起来像什么:

<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>

代码

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);
}

我用我正在使用的正确的 XML 文件格式更新了它。对于那个很抱歉

我试过这个。

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);
    }
}

它仍然显示错误,指出它不是该节点的子节点...

4

1 回答 1

1

试试这个

  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");
        }

旧 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>

新 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>
于 2013-10-17T04:30:43.450 回答