2010-06-30 194 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