XML parsing with <m: properties> tag C #

I want to parse this XML:

<?xml version="1.0" encoding="utf-8" ?> <feed xml:base="http://staging.bradinsight.win.web.emap-intl.net/ALFAPI/AlfWebApi/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <id>http://staging.bradinsight.win.web.emap-intl.net/alfapi/alfwebapi/AuthenticationToken</id> <title type="text">AuthenticationToken</title> <updated>2013-01-22T13:07:10Z</updated> <link rel="self" title="AuthenticationToken" href="AuthenticationToken" /> <entry> <id>http://staging.bradinsight.win.web.emap-intl.net/ALFAPI/AlfWebApi/AuthenticationToken(1)</id> <category term="AlfWebApiInfrastructure.Poco.AuthenticationToken" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <link rel="edit" title="AuthenticationToken" href="AuthenticationToken(1)" /> <title /> <updated>2013-01-22T13:07:10Z</updated> <author> <name /> </author> <content type="application/xml"> <m:properties> <d:AuthenticationTokenId m:type="Edm.Int32">1</d:AuthenticationTokenId> <d:IsValid m:type="Edm.Boolean">true</d:IsValid> <d:Message>Success</d:Message> <d:Token>B50F114BF0BE2B7B210132B1D188DAD8AD6719D808BD59D800509A951C4FB7445687E4F75FCE22C52219495D5172AFFB0FB20FE4E949D20CA0F5055D7F9237C6B5EA4028692A63A2AB0E1703C4668EE2D4C58473A58F1B25AC8ADE4DE12B2A7581064217C5040EDB3A4E302DA1714F21BBEA7D82748B4DF9524C5FED6ED7E265 < /d:Token> </m:properties> </content> </entry> 

Using this code:

  WebRequest wrGETURL; wrGETURL = WebRequest.Create(sURL); StringBuilder output = new StringBuilder(); Stream objStream; try { objStream = wrGETURL.GetResponse().GetResponseStream(); StreamReader objReader = new StreamReader(objStream); // Create an XmlReader using (XmlReader reader = XmlReader.Create(new StringReader(objReader.ReadToEnd()))) { XmlWriterSettings ws = new XmlWriterSettings(); ws.Indent = true; using (XmlWriter writer = XmlWriter.Create(output, ws)) { // Parse the file and display each of the nodes. while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: writer.WriteStartElement(reader.Name); break; case XmlNodeType.Text: writer.WriteString(reader.Value); break; case XmlNodeType.XmlDeclaration: case XmlNodeType.ProcessingInstruction: writer.WriteProcessingInstruction(reader.Name, reader.Value); break; case XmlNodeType.Comment: writer.WriteComment(reader.Value); break; case XmlNodeType.EndElement: writer.WriteFullEndElement(); break; } } } } 

But my C # code cannot parse the tag.

I get this error:

 {System.ArgumentException: Invalid name character in 'm:properties'. The ':' character, hexadecimal value 0x3A, cannot be included in a name. at System.Xml.XmlWellFormedWriter.CheckNCName(String ncname) at System.Xml.XmlWellFormedWriter.WriteStartElement(String prefix, String localName, String ns) at System.Xml.XmlWriter.WriteStartElement(String localName) at AlfApi.Controllers.HomeController.Index() in c:\users\nickgowdy\documents\visual studio 2010\Projects\AlfApi\AlfApi\Controllers\HomeController.cs:line 51} 

Does anyone know how to parse this xml?

Thanks!

0
source share
2 answers

Try replacing the code with the following:

 case XmlNodeType.Element: writer.WriteStartElement(reader.Name.Substring(Math.Max(0,reader.Name.IndexOf(":") + 1))); break; 

Also, after the while use the Flush() method, for example:

 while (reader.Read()) { //process } writer.Flush(); 

Or just use:

 writer.WriteStartElement(reader.LocalName) 
0
source

m: in the tag name denotes the xml namespace. Thus, for parsing xml, you need to declare namespaces - in C # this is done through the XmlNamespaceManager .

Try something like this for the reader.

 // Create an XmlReader NameTable nt = new NameTable(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt); nsmgr.AddNamespace("m", "www.namespacem.com"); // Placeholder uri nsmgr.AddNamespace("d", "www.namespaced.com"); // Placeholder uri XmlParserContext context = new XmlParserContext(nt, nsmgr, null, XmlSpace.None); using (XmlReader reader = XmlReader.Create(new StringReader(x), new XmlReaderSettings(), context)) { // Do funky stuff } 

In addition, you will need to modify the author to resolve the namespace, for example:

 writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI); 
0
source

Source: https://habr.com/ru/post/981635/


All Articles