在xml中创建新节点

时间:2011-08-24 09:05:24

标签: c# xml

有人可以给我一个关于这段代码错误的指针。 我们的想法是查看xml文件,如果找到匹配项,则使用新数据更新现有节点,如果找不到匹配项,则会创建具有所需数据的新节点。 但是,如果我删除创建新节点的代码,而不是只更新节点,它就会创建一个副本。这是循环的问题。?

 ListBoxItem mySelectedItem = listBox5.SelectedItem as ListBoxItem;
        string image = mySelectedItem.Content.ToString();
        XmlDocument xXMLTVDoc = new XmlDocument();
        xXMLTVDoc.Load(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + (@"\Media Center Themer") + @"\MCTDefault.xml");
        XmlNodeList xNodes = xXMLTVDoc.SelectNodes(@"/MediaCenterThemer/Resources/Rcdata/Resource");
        XmlNode xNod = xXMLTVDoc.SelectSingleNode(@"/MediaCenterThemer/Resources/Rcdata/Resource");
        string text1 = "";

        if (listBox5.SelectedItem == null)
        {
            System.Windows.MessageBox.Show("Please select image.", "No Image Selected", MessageBoxButton.OK);
        }
        else if (this.listBox5.SelectedItem != null)
        {
            OpenFileDialog1.Title = "Select your Image";
            OpenFileDialog1.Filter = "PNG|*.png|All files|*.*";
            OpenFileDialog1.RestoreDirectory = true;
            OpenFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

            System.Windows.Forms.DialogResult result = OpenFileDialog1.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                text1 = OpenFileDialog1.FileName;
                try
                {
                    foreach (XmlNode xNode in xNodes)
                    {
                        if (image.Equals("COMMON.BACKGROUND.PNG"))
                        {
                            if (xNode.Attributes[0].Value == "COMMON.BACKGROUND.PNG")
                            {
                                xNode.FirstChild.Attributes[0].Value = text1;
                                xXMLTVDoc.Save(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + (@"\Media Center Themer") + @"\MCTDefault.xml");
                                break;
                            }
                            else if (xNode.Attributes[0].Value != mySelectedItem.Content.ToString())
                            {
                                XmlElement node = xXMLTVDoc.CreateElement("Resource");
                                node.SetAttribute("Id", mySelectedItem.Content.ToString());
                                XmlNode parent1 = xNod.ParentNode;
                                XmlElement nodeTitle = xXMLTVDoc.CreateElement("Replace");
                                nodeTitle.SetAttribute("File", text1);
                                node.AppendChild(nodeTitle);
                                parent1.AppendChild(node);
                                xXMLTVDoc.Save(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + (@"\Media Center Themer") + @"\MCTDefault.xml");
                            }
                        }

                        else if (image.Equals("COMMON.ANIMATED.BACKGROUND.PNG"))
                        {
                            if (xNode.Attributes[0].Value == "COMMON.ANIMATED.BACKGROUND.PNG")
                            {
                                xNode.FirstChild.Attributes[0].Value = text1;
                                xXMLTVDoc.Save(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + (@"\Media Center Themer") + @"\MCTDefault.xml");
                                break;
                            }
                            else if (xNode.Attributes[0].Value != mySelectedItem.Content.ToString())
                            {
                                XmlElement node = xXMLTVDoc.CreateElement("Resource");
                                node.SetAttribute("Id", mySelectedItem.Content.ToString());
                                XmlNode parent1 = xNod.ParentNode;
                                XmlElement nodeTitle = xXMLTVDoc.CreateElement("Replace");
                                nodeTitle.SetAttribute("File", text1);
                                node.AppendChild(nodeTitle);
                                parent1.AppendChild(node);
                                xXMLTVDoc.Save(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + (@"\Media Center Themer") + @"\MCTDefault.xml");
                            }
                        }
                    }
                }

                catch (Exception)
                {
                    MessageBox.Show("Could Not assign Image");
                }

这是xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Change the main colors of Media Center to shades of green -->
<MediaCenterThemer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="MCTSchema.xsd">
<TextFormat>
<!-- Color: OffWhite -->
<Text Color="rgb(242,242,242)">
  <Replace Color="rgb(130,240,130)" />
</Text>
<!-- Color: LightBlue -->
<Text Color="rgb(151,217,255)">
  <Replace Color="rgb(80,200,80)" />
</Text>
<!-- Color: MediumBlue -->
<Text Color="rgb(2,166,212)">
  <Replace Color="rgb(10,160,10)" />
</Text>
</TextFormat>
<Legacy>
<!-- Legacy strings (settings). Color: OffWhite -->
<String Color="rgb(242,242,242)">
  <Replace Color="rgb(115,200,110)" />
</String>
<!-- Legacy strings (settings). Color: LightBlue -->
<String Color="rgb(151,217,255)">
  <Replace Color="rgb(70,150,60)" />
</String>
</Legacy>
<Resources>
<Rcdata>
<!-- Picture Resources. Modification of the default background -->
<Resource Id="COMMON.BACKGROUND.PNG">
<Replace File="C:\Users\Public\Pictures\CITV.png" />
</Resource>
<Resource Id="COMMON.ANIMATED.BACKGROUND.PNG">
<Replace File="Theme\bkg-demo.jpg"/>
</Resource>
</Rcdata>
</Resources>
</MediaCenterThemer>

由于

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情

     bool doesNodeExist = false;
     foreach (XmlNode xNode in xNodes)
                    {
                        if (image.Equals("COMMON.BACKGROUND.PNG"))
                        {
                            if (xNode.Attributes[0].Value == "COMMON.BACKGROUND.PNG")
                            {
doesNodeExist = true;
                                xNode.FirstChild.Attributes[0].Value = text1;
                                xXMLTVDoc.Save(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + (@"\Media Center Themer") + @"\MCTDefault.xml");
                                break;
                            }
                        }
                   }
       if(!doesNodeExist)
      {
          //Create Logic here...
      }