Probleme de lecture XML

jojomillenium Messages postés 136 Date d'inscription samedi 1 mai 2004 Statut Membre Dernière intervention 26 mai 2007 - 26 mai 2007 à 18:40
jojomillenium Messages postés 136 Date d'inscription samedi 1 mai 2004 Statut Membre Dernière intervention 26 mai 2007 - 26 mai 2007 à 18:44
Bonjour,

(C++.NET form application sous VS2003)

J'aimerai pouvoir lire un fichier XML seulment je n y arrive pas, j ai
trouver plusieurs exemple sur le net mais aucun ne fonctionne.

Celui qui me semble le plus simple est le suivant (code ci dessous)
mais lorsqu'il arrive a "while(reader->Read())" il plant et me fait

"An unhandled exception of type 'System.Xml.XmlException' occuredin system.xml.dll

Additional information: Erreur système."


Mais je comprend pas d ou ca peut venir! Lorsque je crée un nouveau
projet console ca fonctionne, mais lorsque je veux l ajouter dans mon
projet la ca plante!


voici le code avec mes headers, biensur je met en commentaire les parties consoles dans mon projet.

Avez vous une idée de ou ca peut venir? Ou connaissez vous une autre facon de faire?

merci beaucoup pour votre aide, ca fait 1 semaines que je plante sur ce point et que j essaie de lire un fichier xml.

#pragma once

#include <CVisionManager_managed.h>

#include <ctime>

#undef MessageBox

#include <windows.h>

#include <msxml.h>

#include <string>

#include "stdafx.h"

#include <tchar.h>

#using <mscorlib.dll>

#using <System.xml.dll>

using namespace System;

using namespace System::IO;

using namespace System::Xml;


void MyXMLReader(void)

{

    XmlTextReader* reader = new XmlTextReader ("config.xml");


    while (reader->Read())

    {

        switch (reader->NodeType)

        {

            case XmlNodeType::Element: // The node is an element.

                Console::Write("<{0}", reader->Name);


                while (reader->MoveToNextAttribute()) // Read the attributes.

                    Console::Write(" {0}='{1}'", reader->Name, reader->Value);

                Console::WriteLine(">");

                break;

            case XmlNodeType::Text: //Display the text in each element.

                Console::WriteLine (reader->Value);

                break;

            case XmlNodeType::EndElement: //Display the end of the element.

                Console::Write("</{0}", reader->Name);

                Console::WriteLine(">");

                break;

        }

    }

    Console::ReadLine();

}

1 réponse

jojomillenium Messages postés 136 Date d'inscription samedi 1 mai 2004 Statut Membre Dernière intervention 26 mai 2007
26 mai 2007 à 18:44
Au fait ma deuxieme alternative que j avais trouvé est la suivante, mais lorsque au lieu de faire

  iXMLDoc->loadXML(CComBSTR(src),&bSuccess); je fait le
iXMLDoc->load(CComVariant(url),&bSuccess); avec comme url "config.xml" ca me sort n importe quoi, il ne le lis pas comme il faut apparement!

Encore merci pour votre aide

#include <windows.h>
#include <msxml.h>
#include <objsafe.h>
#include <objbase.h>
#include
#pragma warning( push )
#pragma warning( disable: 4018 4786)
#include <string>
#pragma warning( pop )
using namespace std;

const wchar_t *src= L""
  L"<?xml version="1.0" encoding="utf-16"?>\r\n"
  L"<root desc="Simple Prog">\r\n"
  L"  <text>Hello World</text>\r\n"
  L"    <layouts>\r\n"
  L"    <lay pos="15" bold="true"/>\r\n"
  L"    <layoff pos="12"/>\r\n"
  L"    <layin pos="17"/>\r\n"
  L"  </layouts>\r\n"
  L"</root>\r\n";

// TElem -- a simple class to wrap up IXMLDomElement and iterat its children.
//   name()    - in stuff it returns "item"
//   val()     - in stuff it returns "stuff"
//   attr(s)   - in stuff it returns "hello"
//   subnode(b)- in hello there it returns the TElem there
//   subval(b) - in hellothere it returns "there"
//   for (TElem c =e.begin(); c!=e.end(); c++) {...} - iterators over the subnodes
struct TElem
{ CComPtr elem;
  CComPtr nlist; int pos; long clen;
  //
  TElem() : elem(0), nlist(0), pos(-1), clen(0) {}
  TElem(int _clen) : elem(0), nlist(0), pos(-1), clen(_clen) {}
  TElem(CComPtr _elem) : elem(_elem), nlist(0), pos(-1), clen(0) {get();}
  TElem(CComPtr _nlist) : elem(0), nlist(_nlist), pos(0), clen(0) {get();}
  void get()
  { if (pos!=-1)
    { elem=0;
      CComPtr inode;
      nlist->get_item(pos,&inode);
      if (inode==0) return;
      DOMNodeType type; inode->get_nodeType(&type);
      if (type!=NODE_ELEMENT) return;
      CComQIPtr e(inode);
      elem=e;
    }
    clen=0; if (elem!=0)
    { CComPtr iNodeList;
      elem->get_childNodes(&iNodeList);
      iNodeList->get_length(&clen); 
    }
  }
  //
  wstring name() const
  { if (!elem) return L"";
    CComBSTR bn; elem->get_tagName(&bn);
    return wstring(bn);
  }
  wstring attr(const wstring name) const
  { if (!elem) return L"";
    CComBSTR bname(name.c_str());
    CComVariant val(VT_EMPTY);
    elem->getAttribute(bname,&val);
    if (val.vt==VT_BSTR) return val.bstrVal;
    return L"";
  }
  bool attrBool(const wstring name,bool def) const
  { wstring a = attr(name);
    if (a==L"true" || a==L"TRUE") return true;
    else if (a==L"false" || a==L"FALSE") return false;
    else return def;
  }
  int attrInt(const wstring name, int def) const
  { wstring a = attr(name);
    int i, res=swscanf(a.c_str(),L"%i",&i);
    if (res==1) return i; else return def;
  }
  wstring val() const
  { if (!elem) return L"";
    CComVariant val(VT_EMPTY);
    elem->get_nodeTypedValue(&val);
    if (val.vt==VT_BSTR) return val.bstrVal;
    return L"";
  }
  TElem subnode(const wstring name) const
  { if (!elem) return TElem();
    for (TElem c=begin(); c!=end(); c++)
    { if (c.name()==name) return c;
    }
    return TElem();
  }
  wstring subval(const wstring name) const
  { if (!elem) return L"";
    TElem c=subnode(name);
    return c.val();
  }
  TElem begin() const
  { if (!elem) return TElem();
    CComPtr iNodeList;
    elem->get_childNodes(&iNodeList);
    return TElem(iNodeList);
  }
  TElem end() const
  { return TElem(clen);
  }
  TElem operator++(int)
  { if (pos!=-1) {pos++; get();}
    return *this;
  }
  bool operator!=(const TElem &e) const
  { return pos!=e.clen;
  }
};

void test()
{
  CComPtr iXMLDoc;
  iXMLDoc.CoCreateInstance(__uuidof(DOMDocument));
    
  // Following is a bugfix for PocketPC.
#ifdef _UNDER_CE
  gargle bargle
  iXMLDoc->put_async(VARIANT_FALSE);
  CComQIPtr iSafety(iXMLDoc);
  if (iSafety)
  { DWORD dwSupported, dwEnabled;
    iSafety->GetInterfaceSafetyOptions(IID_IXMLDOMDocument,&dwSupported,&dwEnabled);
    iSafety->SetInterfaceSafetyOptions(IID_IXMLDOMDocument,dwSupported,0);
  }
#endif

  // Load the file.
  VARIANT_BOOL bSuccess=false;
  // Can load it from a url/filename...
  //iXMLDoc->load(CComVariant(url),&bSuccess);
  // or from a BSTR...
  iXMLDoc->loadXML(CComBSTR(src),&bSuccess);

  // Get a pointer to the root
  CComPtr iRootElm;
  iXMLDoc->get_documentElement(&iRootElm);

  // Thanks to the magic of CComPtr, we never need call
  // Release() -- that gets done automatically.

  TElem eroot(iRootElm);
  wstring desc = eroot.attr(L"desc");
  // returns "Simple Prog"

  TElem etext = eroot.subnode(L"text");
  wstring s = etext.val();
  // returns "Hello World"
  s = eroot.subval(L"text");
  // This is a shorter way to achieve the same thing

  TElem elays = eroot.subnode(L"layouts");
  for (TElem e=elays.begin(); e!=elays.end(); e++)
  { int pos = e.attrInt(L"pos",-1);
    bool bold = e.attrBool(L"bold",false);
    // we suggest default values, in case the attribute is missing
    wstring id = e.name();
    // returns "lay" or "layoff" or "layin"
  }
}

#ifdef UNDER_CE
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPTSTR,int)
{ CoInitializeEx(NULL,COINIT_MULTITHREADED);
  test();
  CoUninitialize();
  return 0;
}
#else
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{ CoInitialize(NULL);
  test();
  CoUninitialize();
  return 0;
}
#endif
0
Rejoignez-nous