你好,我試圖編寫一個XML解析器導入我的新項目的配置文件。但是,我得到一個內存溢出與一個功能誰正在工作,所有的時間才適當。XML解析器內存溢出
在「距離= getFirstMatchBetweenKeyword(interfaceText,」距離「);」;在進口接口方法
這是Importer.class:
class Importer
{
string path;
List<Router> routerlist = new List<Router>();
StreamReader sr;
public Importer(string newPath)
{
path = newPath;
sr = new StreamReader(path);
}
public List<Router> getConfig()
{
getNodes(loadFile());
return routerlist;
}
string loadFile()
{
string temp;
//StreamReader sr = new StreamReader(path);
temp = sr.ReadToEnd().TrimEnd(' ');
temp.TrimStart(' ');
return temp;
}
void getNodes(string importText)
{
string nodetext = getFirstMatchBetweenKeyword(importText, "Nodelist");
while (nodetext.Length > 0)
{
importNode(getFirstMatchBetweenKeyword(nodetext, "Node"));
nodetext = delTextUntil(nodetext, "Node");
}
}
void importNode(string nodeText) {
Router rtr;
Interface intr;
string name;
name = getFirstMatchBetweenKeyword(nodeText, "Name");
rtr = new Router(name);
while (nodeText.Length > 0)
{
intr = importInterface(getFirstMatchBetweenKeyword(nodeText, "Interface"));
nodeText = delTextUntil(nodeText, "Interface");
rtr.setInterface(intr.Interf, intr.Network, intr.Distance);
}
routerlist.Add(rtr);
}
Interface importInterface(string interfaceText)
{
Interface inter;
string name;
string network;
string distance;
name = getFirstMatchBetweenKeyword(interfaceText, "Interfacename");
network = getFirstMatchBetweenKeyword(interfaceText, "Network");
distance = getFirstMatchBetweenKeyword(interfaceText, "Distance");
inter = new Interface(name, network, distance);
return inter;
}
string getFirstMatchBetweenKeyword(string text, string keyword)
{
string starting = "<" + keyword + ">";
string ending = "</" + keyword + ">";
char[] oldText;
char[] start;
char[] end;
char[] newText;
int helper = 0;
int bufferindex = 0;
bool startParse = false;
bool endParse = false;
oldText = text.ToCharArray();
start = starting.ToCharArray();
end = ending.ToCharArray();
newText = new char[oldText.Length];
string output;
for (int i = 0; i < oldText.Length; i++)
{
if (startParse)
{
if (endParse)
{
int delcounter = end.Length;
for (int ii = 0 ; ii < delcounter; ii++)
{
newText[bufferindex] = ' ';
bufferindex--;
}
break;
}
else
{
newText[bufferindex] = oldText[i];
bufferindex++;
if (oldText[i] == end[helper])
{
helper++;
if (helper == end.Length)
{
endParse = true;
}
}
else
{
helper = 0;
}
}
}
else
{
if (oldText[i] == start[helper])
{
helper++;
if(helper == start.Length){
startParse = true;
helper = 0;
}
}
else
{
helper = 0;
}
}
}
output = new string(newText);
return output.TrimEnd(' ');
}
string delTextUntil(string oldText, string keyword)
{
keyword = "</" + keyword + ">";
char[] old = oldText.ToCharArray();
char[] key = keyword.ToCharArray();
int keyindex = 0;
for (int i = 0; i < old.Length; i++)
{
old[i] = ' ';
if (oldText[i] == keyword[keyindex])
{
keyindex++;
if (keyindex == keyword.Length)
{
break;
}
}
else {
keyindex = 0;
}
}
keyword = new string(old);
keyword.TrimStart(' ');
return keyword;
}
}
,如果你需要,這裏的接口和Router類的定義:
class Router {
string name = "";
List<Interface> nodeList = new List<Interface>();
public Router(string newName)
{
name = newName;
}
public string Name
{
get { return name; }
set { name = value; }
}
public void setInterface(string newInterface, string newNetwork, string newDistance)
{
Interface rnetdis = new Interface(newInterface, newNetwork, newDistance);
nodeList.Add(rnetdis);
}
public List<Interface> getRouterInterfaces()
{
return nodeList;
}
public bool delNetwork(string networkName)
{
for (int i = 0; i < nodeList.Count(); i++)
{
if (nodeList[i].Network == networkName)
{
nodeList.RemoveAt(i);
return true;
}
}
return false;
}
}
class Interface {
string interf;
string network;
string distance;
public Interface(string newInterf)
{
interf = newInterf;
}
public Interface(string newInterf, string newNetwork, string newDistance)
{
interf = newInterf;
network = newNetwork;
distance = newDistance;
}
public string Interf
{
set{
interf = value;
}
get
{
return interf;
}
}
public string Network
{
set
{
network = value;
}
get
{
return network;
}
}
public string Distance
{
set
{
distance = value;
}
get
{
return distance;
}
}
}
的配置我想分析看起來像這樣:
<Nodelist>
<Node>
<Name>r1</Name>
<Interface>
<Interfacename>inter1</Interfacename>
<Network>5</Network>
<Distance>50</Distance>
</Interface>
<Interface>
<Interfacename>inter2</Interfacename>
<Network>2</Network>
<Distance>20</Distance>
</Interface>
</Node>
<Node>
<Name>r2</Name>
<Interface>
<Interfacename>inter1</Interfacename>
<Network>5</Network>
<Distance>50</Distance>
</Interface>
</Node>
<Node>
<Name>r3</Name>
<Interface>
<Interfacename>inter1</Interfacename>
<Network>2</Network>
<Distance>20</Distance>
</Interface>
</Node>
</Nodelist>
希望有人能幫助我。
非常感謝提前,
克里斯
任何你沒有使用的原因[XmlDocument](https://msdn.microsoft.com/en-us/library/system.xml.xmldocument%28v=vs.110%29 .aspx)或[XDocument](https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument%28v=vs.110%29.aspx)? – adv12