繁体   English   中英

XML节点始终为null

[英]XML node is always null

以下函数不返回我想要的节点的值,即“ CompanyPolicyId”。 我已经尝试了很多事情,但仍然无法正常工作。 有人知道这可能是什么问题吗?

 public void getpolicy(string rootURL, string policyNumber)
        {
            string basePolicyNumber = policyNumber.Remove(policyNumber.Length - 2);
            basePolicyNumber = basePolicyNumber + "00";

            using (WebClient client = new WebClient())
            {
                NetworkCredential credentials = new NetworkCredential();
                credentials.UserName = AppVars.Username;
                credentials.Password = AppVars.Password;
                client.Credentials = credentials;

                try
                {
                    XmlDocument doc = new XmlDocument();

                    doc.LoadXml(client.DownloadString(rootURL + basePolicyNumber));
                    XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
                    mgr.AddNamespace("zzzlocal", "http://com.zzz100.policy.data.local");

                    // Select the Identifier node with a 'name' attribute having an 'id' value
                    var node = doc.DocumentElement.SelectSingleNode("/InsurancePolicy/Indentifiers/Identifier[@name='CompanyPolicyId']", mgr);
                    if (node != null && node.Attributes["value"] != null)
                    {
                        // Pick out the 'value' attribute's value
                        var val = node.Attributes["value"].Value;

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            } 

这是XML文档:

<InsurancePolicy xmlns:zzzlocal="com.zzz100.policy.data.local" schemaVersion="2.7" variant="multiterm">
<Identifiers>
<Identifier name="VendorPolicyId" value="AAAA"/>
<Identifier name="CompanyPolicyId" value="BBBB"/>
<Identifier name="QuoteNumber" value="CCCC"/>
<Identifier name="pxServerIndex" value="DDDD"/>
<Identifier name="PolicyID" value="EEEE"/>
</Identifiers>
</InsurancePolicy>

最近6个小时,我一直在尝试解决此问题。 老实说,这很烂。

尝试使用这个

//Identifier[@name='CompanyPolicyId']"

或下面的其他方法

XElement rootElement = XElement.Load(<url here>);
string targetValue =
  (string)rootElement.Elements("Identifier")
  .Single(e => (string)e.Attribute("name") == "CompanyPolicyId")
  .Attribute("value");

这假设您希望能够通过名称来定位一个Identifier节点,并确保将有一个具有该名称的元素。 如果不正确,那么如果未找到该节点,则.Single调用将引发异常。

如果您需要使用凭据并想使用WebClient,则可以使用以下方法:(注意,我没有做任何异常处理,检查流可用性或以其他方式处置/关闭流,这只是如何获取示例来“工作”)

string uri = "> url here! <";
System.Net.WebClient wc = new System.Net.WebClient();
StreamReader sr = new StreamReader(wc.OpenRead(uri));
string xml = sr.ReadToEnd();
XElement rootElement = XElement.Parse(xml);
string targetValue =
  (string)rootElement.Elements("Identifier")
  .Single(e => (string)e.Attribute("name") == "CompanyPolicyId")
  .Attribute("value");

这是更简单的版本

    [Test]
    public void Test()
    {
        XElement root = XElement.Load(@"C:\1.xml");
        XElement identifier = GetIdentifierByName(root, "CompanyPolicyId");
        if (identifier == null)
        {
            return;
        }
        Console.WriteLine(identifier.Attribute("value"));
    }

    private static XElement GetIdentifierByName(XContainer root, string name)
    {
        return root.Descendants()
            .Where(x => x.Name.LocalName == "Identifier")
            .FirstOrDefault(x => x.Attribute("name").Value == name);
    }
}

控制台输出为BBBB

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM