0

我想解析这个 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>

使用此代码:

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

                    }
                }

但是我的 C# 代码无法解析 tag 。

我收到此错误:

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

有谁知道如何解析这个xml?

谢谢!

4

2 回答 2

0

尝试用以下代码替换您的代码:

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

此外,在while循环之后,使用该Flush()方法,如下所示:

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

或者只是使用:

writer.WriteStartElement(reader.LocalName)
于 2013-01-22T15:48:22.293 回答
0

标记名称中的m:表示一个 xml 命名空间。因此,要解析 xml,您需要声明命名空间 - 在 c# 中,这是通过XmlNamespaceManager完成的。

为读者尝试这样的事情

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

此外,您将需要修改您的编写器以允许名称空间,例如:

writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
于 2013-01-22T15:52:58.883 回答