开发者

xml parsing problem in c#

<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>

i have an xml file i want parse this x,l file and then want to store in an object how it is possible

i have used parsing technique like this

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++;
}


The most efficient way to do this is to use the xsd.exe tool which is part of the Windows SDK. You should have a xsd.exe in your Visual Studio prompts somewhere - if not, it would be located in a directory C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ (or similar, depends on your machine and OS version).

Using xsd.exe, you can turn your XML into a XSD file (XML schema):

 xsd yourfile.xml

This will give you a yourfile.xsd schema file. Use xsd.exe a second time, you can turn that into a C# class:

 xsd yourfile.xsd /c

and now you should have a yourfile.cs that contains a class definition that's able to deserialize that file into objects.

To do so, use something like this:

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

var result = ser.Deserialize(xr);

and you should have your XML represented as a C# object now.


Use or Google "XML Serialization" in the .NET Framework see the link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜