C# 如何在Xml文件中的特定节点之后添加Xml节点

C# 如何在Xml文件中的特定节点之后添加Xml节点,c#,xml,C#,Xml,我想添加将图像添加到文件的功能。 我有将图像转换为字符串并返回到图像的功能,效果很好 XML文件数据 <MovieData> <Movie> <Name>Death Race</Name> <Type>Action</Type> <Type>Adventure</Type> <Rating>R</Rating&g

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

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文件格式更新了它。对不起

我试过这个

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);
    }
}
它仍然显示错误,表示它不是该节点的子节点…

试试这个

  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");
        }
XmlDocument xDoc=new XmlDocument();
加载(“E:\\test.xml”);
XmlNodeList xE=xDoc.SelectNodes(“//MovieData/Movie/LengthMin”);
Dictionary dicMovieData=null;
如果(xE!=null)
{
对于(int-iVal=0;iVal0)
{
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)
{
xmlementxnewchild=xDoc.CreateElement(“图像”);
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>

死亡赛跑
行动
冒险
R
蓝光
不适用
不适用
电影系列
1.
51
晚上10:44:23
10/13/2013
死亡赛跑2
行动
冒险
R
联合体
不适用
不适用
电影系列
1.
41
晚上9:52:34
10/9/2013
新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>

死亡赛跑
行动
冒险
R
蓝光
不适用
不适用
电影系列
1.
51
string///已添加节点
晚上10:44:23
10/13/2013
死亡赛跑2
行动
冒险
R
联合体
不适用
不适用
电影系列
1.
41
晚上9:52:34
10/9/2013
试试这个

  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");
        }
XmlDocument xDoc=new XmlDocument();
加载(“E:\\test.xml”);
XmlNodeList xE=xDoc.SelectNodes(“//MovieData/Movie/LengthMin”);
Dictionary dicMovieData=null;
如果(xE!=null)
{
对于(int-iVal=0;iVal0)
{
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)
{
xmlementxnewchild=xDoc.CreateElement(“图像”);
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>

死亡赛跑
行动
冒险
R
蓝光
不适用
不适用
电影系列
1.
51
晚上10:44:23
10/13/2013
死亡赛跑2
行动
冒险
R
联合体
不适用
不适用
电影系列
1.
41
晚上9:52:34
10/9/2013
新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>

死亡赛跑
行动
冒险
R
蓝光
不适用
不适用
电影系列
1.
51
string///已添加节点
晚上10:44:23
10/13/2013
死亡赛跑2
行动
冒险
R
联合体
不适用
不适用
电影系列
1.
41
晚上9:52:34
10/9/2013