Skip to content

Instantly share code, notes, and snippets.

@imZack
Last active December 15, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imZack/5344046 to your computer and use it in GitHub Desktop.
Save imZack/5344046 to your computer and use it in GitHub Desktop.
C# XML Save & Load
private void load()
{
XmlDocument doc = new XmlDocument();
doc.Load(rootPath + "\\config.xml");
XmlElement root = doc.DocumentElement;
name = root.GetAttribute("name");
XmlNodeList objs = root.SelectNodes("/ThemeConfig/Objects/Image");
foreach (XmlElement elem in objs)
{
//Console.WriteLine(elem.GetAttribute("name"));
ImageObj imgObj = new ImageObj(elem.GetAttribute("name"), elem.GetAttribute("img_name"), elem.GetAttribute("depth_name"));
imgObjs.Add(imgObj.name, imgObj);
}
}
private void create()
{
XmlDocument doc = new XmlDocument();
XmlElement themeConfig = doc.CreateElement("ThemeConfig");
themeConfig.SetAttribute("name", name);
XmlElement Objects = doc.CreateElement("Objects");
XmlElement Background = doc.CreateElement("Background"); ;
Background.SetAttribute("name", "Background");
Background.SetAttribute("img_name", "");
Objects.AppendChild(Background);
themeConfig.AppendChild(Objects);
doc.AppendChild(themeConfig);
doc.Save(rootPath + "\\config.xml");
}
public void save()
{
XmlDocument doc = new XmlDocument();
XmlElement themeConfig = doc.CreateElement("ThemeConfig");
themeConfig.SetAttribute("name", name);
XmlElement Objects = doc.CreateElement("Objects");
foreach (ImageObj imgObj in imgObjs.Values)
{
XmlElement Image = doc.CreateElement("Image");
Image.SetAttribute("name", imgObj.name);
Image.SetAttribute("img_name", imgObj.img_name);
Image.SetAttribute("depth_name", imgObj.depth_name);
Objects.AppendChild(Image);
}
themeConfig.AppendChild(Objects);
doc.AppendChild(themeConfig);
doc.Save(rootPath + "\\config.xml");
}
<ThemeConfig name="小美人魚">
<Objects>
<Image name="miney" img_name="miney.png" depth_name="VerRec6.png" />
<Image name="micky" img_name="micky.png" depth_name="VerRec5.png" />
<Image name="(Background)" img_name="IMG_1744.jpg" depth_name="" />
<Image name="lksdjfks" img_name="bus.png" depth_name="d_HorRec1.png" />
</Objects>
</ThemeConfig>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment