我想添加将图像添加到文件的功能。我具有将图像转换为字符串并返回图像的功能,效果很好。
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);
}
}
它仍然显示错误,指出它不是该节点的子节点...