Question

I have XML file that contains services names in windows7, one of the services has white space i.e "service name" I get exception when I load the file:

fileName = file;
pathToFile = path;
XmlDocument ServerList = new XmlDocument();            
ServerList.Load(pathToFile + fileName);

the XML:

<systems>
 <Groups>
  <Myervices>    
     <Dialogic/>
     <BoardServer/>
     <HmpElements/>
     <Service-1 Agent/>
   </Myervices>
 </Groups>
</systems>

the filenName has the white space, is there a way to receive it cause I cannot change the service name.

the exception I get:

'/' is an unexpected token. The expected token is '='. Line 824, position 23. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args) at System.Xml.XmlTextReaderImpl.ThrowUnexpectedToken(String expectedToken1, String expectedToken2) at System.Xml.XmlTextReaderImpl.ParseAttributes() at System.Xml.XmlTextReaderImpl.ParseElement() at System.Xml.XmlTextReaderImpl.ParseElementContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at System.Xml.XmlDocument.Load(String filename) at Stop_Start_systems.Functions..ctor(String path, String file) in c:\Stop_Start_systems\Functions.cs:line 32 at Stop_Start_systems.Default.Page_Load(Object sender, EventArgs e) in c:\Stop_Start_systems\Default.aspx.cs:line 31 System.Collections.ListDictionaryInterna Thanks

Was it helpful?

Solution

The problem has nothing to do with the name of the XML file, or the code you posted. It has everything to do with the XML being invalid. XML element names can't contain spaces, so this isn't valid:

<Service-1 Agent/>

Instead, you should use the same element name for all services, putting the service name into an attribute instead, e.g.

<Service Name="Service-1 Agent" />
<Service Name="Some other service" />

etc. I would strongly advise you to create the XML file automatically using an API instead of by hand - that way you're much more likely to end up with valid XML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top