C#XML解析错误:未将对象引用设置为对象的实例

时间:2013-04-24 20:30:55

标签: c# xml xml-parsing

我正在尝试获取以下XML结构中<d:neu_UniqueId><d:Name>元素的值。我正在处理的程序调用WCF Web服务,并根据用户输入的搜索条件返回带有此信息列表的XML文档(我已删除隐私值) 。

<entry>
<id xmlns=\"http://www.w3.org/2005/Atom\">http://quahildy01/xRMDRMA02/XRMServices/2011/OrganizationData.svc</id>
<title type=\"text\" xmlns=\"http://www.w3.org/2005/Atom\">somethingHere</title>
<updated xmlns=\"http://www.w3.org/2005/Atom\">2013-04-24T17:15:45Z</updated>
<author xmlns=\"http://www.w3.org/2005/Atom\"><name /></author>
<link rel=\"edit\" title=\"Account\" href=\"AccountSet(guid'aa2232f4-418a-e111-9710-005056a8161c')\" xmlns=\"http://www.w3.org/2005/Atom\" />
<category term=\"Microsoft.Crm.Sdk.Data.Services.Account\" scheme=\"http://schemas.microsoft.com/ado/2007/08/dataservices/scheme\" xmlns=\"http://www.w3.org/2005/Atom\" />
<content type=\"application/xml\" xmlns=\"http://www.w3.org/2005/Atom\">
    <m:properties xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\">
        <d:neu_UniqueId xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\">123</d:neu_UniqueId>
        <d:AccountId m:type=\"Edm.Guid\" xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\">1</d:AccountId>
        <d:Name xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\">SomethingInHere</d:Name>
    </m:properties>
</content>
</entry>

这是我正在使用的C#代码。当我单步执行代码时,我可以在childNode变量中看到正确的值,但是当程序在第一个.InnerText()方法上移动时,我收到此错误:

NullReferenceException: Object reference not set to an instance of an object.

这是代码

        try
        {
            WebRequest myWebRequest = WebRequest.Create(URL);
            myWebRequest.PreAuthenticate = true;
            myWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;

            WebResponse myWebResponse = myWebRequest.GetResponse();
            Stream myFileStreamResult = myWebResponse.GetResponseStream();
            Encoding encoder = System.Text.Encoding.GetEncoding("utf-8");
            StreamReader readStream = new StreamReader(myFileStreamResult, encoder);

            results = readStream.ReadToEnd();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(results);

            XmlNodeList parentNode = xmlDoc.GetElementsByTagName("entry");

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
            nsmgr.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
            nsmgr.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");

            string accountName2 = xmlDoc.SelectSingleNode("entry/content/m:properties/d:Name", nsmgr).InnerText;

            foreach (XmlNode childNode in parentNode)
            {
                string accountName = childNode.SelectSingleNode("/content/m:properties/d:Name", nsmgr).InnerText;
                string uniqueId = childNode.SelectSingleNode("/content/m:properties/d:neu_UniqueId", nsmgr).InnerText;
            }

        }

修改

看起来这是从Web服务返回的XML的问题。对于每个元素,xmlns属性在值之前包含\个字符。

1 个答案:

答案 0 :(得分:1)

尝试为“http://www.w3.org/2005/Atom”添加命名空间条目,并在XPath中使用它。还要在foreach中的SelectSingleNode调用中删除XPath中的第一个'/'字符。

编辑

我在名为response.xml的文件中设置了xml,它看起来如下所示。删除了你的一些反斜杠以使其可行。

<?xml version="1.0" encoding="UTF-8"?>
<entry>
  <id xmlns="http://www.w3.org/2005/Atom">http://quahildy01/xRMDRMA02/XRMServices/2011/OrganizationData.svc</id>
  <title type="text" xmlns="http://www.w3.org/2005/Atom">somethingHere</title>
  <updated xmlns="http://www.w3.org/2005/Atom">2013-04-24T17:15:45Z</updated>
  <author xmlns="http://www.w3.org/2005/Atom">
    <name />
  </author>
  <link rel="edit" title="Account" href="AccountSet(guid'aa2232f4-418a-e111-9710-005056a8161c')" xmlns="http://www.w3.org/2005/Atom" />
  <category term="Microsoft.Crm.Sdk.Data.Services.Account" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" xmlns="http://www.w3.org/2005/Atom" />
  <content type="application/xml" xmlns="http://www.w3.org/2005/Atom">
    <m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
      <d:neu_UniqueId xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">123</d:neu_UniqueId>
      <d:AccountId m:type="Edm.Guid" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">1</d:AccountId>
      <d:Name xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">SomethingInHere</d:Name>
    </m:properties>
  </content>
</entry>

我在文件中读到并用下面的代码解析它,它对我有用。

        string path = @"response.xml";
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(path);

        XmlNodeList parentNode = xmlDoc.GetElementsByTagName("entry");

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
        nsmgr.AddNamespace("f", "http://www.w3.org/2005/Atom");
        nsmgr.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
        nsmgr.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");

        var accountName2 = xmlDoc.SelectSingleNode("entry/f:content/m:properties/d:Name", nsmgr).InnerText;

        foreach (XmlNode childNode in parentNode)
        {
            string accountName = childNode.SelectSingleNode("f:content/m:properties/d:Name", nsmgr).InnerText;
            string uniqueId = childNode.SelectSingleNode("f:content/m:properties/d:neu_UniqueId", nsmgr).InnerText;
        }