2010-06-30 108 views
0
<Root> 
    <Application> 
    <AppName>App1</AppName> 
    <IdMark>ClassName</IdMark> 
    <ClassName>Notepad</ClassName> 
    <ExecName>Notepad</ExecName> 
    <Mod> 
    <ModName>HookPrintAPIs</ModName> 
    <Api> 
     <ApiName>TextOutA</ApiName> 
     <ApiName>TextOutW</ApiName> 
     <ApiName>ExtTextOutA</ApiName> 
     <ApiName>ExtTextOutW</ApiName> 
     <ApiName>DrawTextA</ApiName> 
     <ApiName>DrawTextW</ApiName> 
     <ApiName>DrawTextExA</ApiName> 
     <ApiName>DrawTextExW</ApiName> 
     <ApiName>GdiDrawString</ApiName> 
    </Api> 
    </Mod> 
    </Application> 
</Root> 

我有一個XML文件,我想分析此X,L文件,然後要在一個對象來存儲它是如何可能XML解析問題

我已經使用解析技術這樣

ArrayList oArrayListForModuleName = new ArrayList(); 
ArrayList oArrayListClass = new ArrayList(); 
ArrayList oArrayListApi = new ArrayList(); 
List<string> oListofApiName1 = new List<string>(); 
while (oRreader.Read()) 
{ 
    if (oRreader.NodeType == XmlNodeType.Element) 
    { 
    switch (oRreader.Name) 
     { 
     case "AppName": 
         oRreader.Read(); 
         oArrayListClass.Add(oRreader.Value); 
         break; 
     case "IdMark": 
         oRreader.Read(); 
         oArrayListClass.Add(oRreader.Value); 
         break; 
     case "ClassName": 
         oRreader.Read(); 
         oArrayListClass.Add(oRreader.Value); 
         break; 
     case "ExecName": 
         oRreader.Read(); 
         oArrayListClass.Add(oRreader.Value); 
         break; 
     case "ModName": 
         oRreader.Read(); 
         oArrayListForModuleName.Add(oRreader.Value); 
         break; 
     case "ApiName": 
         oRreader.Read(); 
         oArrayListApi.Add(oRreader.Value); 
         oListofApiName1.Add(oRreader.Value); 
         break; 
        } 
       } 
      } 
oRreader.Close(); 
for (int i = 0; i < oArrayListApi.Count; i++) 
{ 
if (oArrayListApi[i].Equals("Not Set")) 
    { 
     oArrayListApi[i] = ""; 
    } 
} 

for (int i = 0; i < oArrayListForModuleName.Count; i++) 
{ 
    if (oArrayListForModuleName[i].Equals("Not Set")) 
    { 
    oArrayListForModuleName[i] = ""; 
    } 
} 
int counter = 0; 
int countformod = 0; 
while (j < oArrayListClass.Count) 
    { 
    CConfigManager oXmlHook = new CConfigManager(); 
    oXmlHook.uAppName = oArrayListClass[j].ToString(); 
    oXmlHook.uIdMark = oArrayListClass[++j].ToString(); 
    oXmlHook.uClassName = oArrayListClass[++j].ToString(); 
    oXmlHook.uExecName = oArrayListClass[++j].ToString(); 
    for (int cntr = 0; cntr < countofapi; cntr++) 
     { 
     oXmlHook.oListOfApiName.Add(oArrayListApi[counter].ToString()); 
     counter++; 
     } 
    for (int c = 0; c < countofmodule; c++) 
     { 
     oXmlHook.oListOfModuleName.Add(oArrayListForModuleName[countformod].ToString()); 
     countformod++; 
     } 
    oArrListOfObject.Add(oXmlHook); 
    j++; 
} 
+0

我會使用XmlDocument或XElement而不是手動解析 - 如果你不能使用這些,你可能想解釋爲什麼。 – 2010-06-30 05:53:04

回答

2

最有效的方式做到這一點使用或谷歌「XML序列化」是使用xsd.exe工具是Windows SDK的一部分。您的Visual Studio提示符應該有xsd.exe - 如果沒有,它將位於目錄C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\(或類似的,取決於您的機器和操作系統版本)。

使用xsd.exe,你可以把你的XML轉換爲XSD文件(XML架構):

xsd yourfile.xml 

這會給你一個yourfile.xsd模式文件。使用xsd.exe第二次,你可以把它轉換成一個C#類:

xsd yourfile.xsd /c 

,現在你應該有一個yourfile.cs包含類定義這是能夠將該文件反序列化爲對象。

要做到這一點,使用這樣的:

XmlSerializer ser = new XmlSerializer(typeof(Root)); 
XmlReader xr = XmlReader.Create("YourFile.xml"); 

var result = ser.Deserialize(xr); 

,你應該有你的XML現在表示爲C#對象。

+0

我總是習慣於自己創建我的課程,從來不知道這個工具存在!感謝讓我的生活變得更輕鬆:D – Peter 2010-06-30 06:35:58