0
有人能給我一個關於這個代碼有什麼問題的指針。 這個想法是,如果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>
感謝
的編程語言是這樣嗎? –
抱歉我用C#寫的不好。 – Kevin
爲C#重新標記。不是一個.NET程序員,所以我不認爲我可以幫你。如果沒有人提出答案(我會很驚訝),稍後我會看一看。 –