flash读取xml加id的:根据XMl文件的ID读取对应的内容

项目中经常遇到些读取xml文件思路方法特别是在进行处理情况下要根据ErrId来获取Error内容网上这方面东西比较零散今天花了点时间整理了写了个小例子拿来和大家分享

首先建立个共同代码如下:

view plaincopy to clipboardpr?
using ;
using .Collections.Generic;
using .Linq;
using .Text;
using .Xml;
using .IO;

XmlRead
{
public Common
{
public bool GetMessageByKey( xmlPath, AppKey, out AppValue)
{
bool isSuccess = true;
AppValue = "";
(!IsXmlFlieExist(xmlPath))
{
false;
}
try
{
XmlDocument xDoc = XmlDocument;
xDoc.Load(xmlPath);
XmlNode xNode;
XmlElement xElem1;

xNode = xDoc.SelectSingleNode("//appSettings");

xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
(xElem1 != null)
{
AppValue = xElem1.GetAttribute("value");
}

{
}
}
catch (Exception ex)
{
ex.;
isSuccess = false;
}
isSuccess;
}


public bool IsXmlFlieExist( xmlPath)
{
try
{
(File.Exists(xmlPath))
{
true;
}

{
false;
}

}
catch
{
false;
}
}

}
}
using ;
using .Collections.Generic;
using .Linq;
using .Text;
using .Xml;
using .IO;

XmlRead
{
public Common
{
public bool GetMessageByKey( xmlPath, AppKey, out AppValue)
{
bool isSuccess = true;
AppValue = "";
(!IsXmlFlieExist(xmlPath))
{
false;
}
try
{
XmlDocument xDoc = XmlDocument;
xDoc.Load(xmlPath);
XmlNode xNode;
XmlElement xElem1;

xNode = xDoc.SelectSingleNode("//appSettings");

xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
(xElem1 != null)
{
AppValue = xElem1.GetAttribute("value");
}

{
}
}
catch (Exception ex)
{
ex.;
isSuccess = false;
}
isSuccess;
}


public bool IsXmlFlieExist( xmlPath)
{
try
{
(File.Exists(xmlPath))
{
true;
}

{
false;
}

}
catch
{
false;
}
}

}
}


是下面这样比较简单:

view plaincopy to clipboardpr?
using ;
using .Collections.Generic;
using .Linq;
using .Text;
using .Xml;

XmlRead
{
Program
{
void Main( args)
{
msg;
const xmlPath = "D:\\VS-workspace\\XmlRead\\XmlRead\\MyConfig.xml";
Common .GetMessageByKey(xmlPath, "Errer001", out msg);
Console.WriteLine(msg);
Console.ReadKey;
}
}
}
using ;
using .Collections.Generic;
using .Linq;
using .Text;
using .Xml;

XmlRead
{
Program
{
void Main( args)
{
msg;
const xmlPath = "D:\\VS-workspace\\XmlRead\\XmlRead\\MyConfig.xml";
Common .GetMessageByKey(xmlPath, "Errer001", out msg);
Console.WriteLine(msg);
Console.ReadKey;
}
}
}



参考xml文件如下:



view plaincopy to clipboardpr?
<?xml version="1.0" encoding="utf-8"?>
<.Config>
<appSettings>
<add key="ConnectString" value="D085D536F765EEB74123E527CEC0F564" />
<add key="Message2" value="D085D536F765EEB74123E527CEC0F564" />
<add key="Message3" value="D085D536F765EEB74123E527CEC0F564" />

<add key="Errer001" value="Host is already using!" />
<add key="Errer002" value="Please input halfsize number!" />
</appSettings>
</.Config>
<?xml version="1.0" encoding="utf-8"?>
<.Config>
<appSettings>
<add key="ConnectString" value="D085D536F765EEB74123E527CEC0F564" />
<add key="Message2" value="D085D536F765EEB74123E527CEC0F564" />
<add key="Message3" value="D085D536F765EEB74123E527CEC0F564" />

<add key="Errer001" value="Host is already using!" />
<add key="Errer002" value="Please input halfsize number!" />
</appSettings>
</.Config>


执行结果:

执行结果

Tags:  js读取xml java读取xml 读取xml flash读取xml加id的

延伸阅读

最新评论

发表评论