System.Xml.Linq.XContainer.Element(…) 返回了空值。

huangapple go评论67阅读模式
英文:

System.Xml.Linq.XContainer.Element(...) returned null

问题

I have postXmlData method to post request and get response in Xml. After receiveng responce I am trying to show attributes name startswith ("f"), but getting error System.Xml.Linq.XContainer.Element(...) returned null. What am I doing wrong?

Response from Request:

<response result="0">
	<prov>
		<getStatus result="0">
			<pay date="2023-02-08T19:44:33+03:00" fatal="false" id="8022140013003" result="0" status="2" uid="26775263057008" value-date="2023-02-08T19:44:40+03:00">
			</pay>
		</getStatus>
	</prov>
</response>

To show attributes I am using XElement:

XmlDocument doc = postXMLData(Requests.getStatus("08022140013003"));
XElement e = XElement.Load(new XmlNodeReader(doc));
Console.WriteLine(e); //here ok

IEnumerable<XAttribute> attrs1 = e.Element("response").Element("prov").Element("getStatus").Element("pay")?.Attributes().Where(a => a.Name.LocalName.StartsWith("f"));
Console.WriteLine(attrs1);
Console.ReadKey();

I have removed the HTML entities (&quot;) and provided the text and code as requested.

英文:

I have postXmlData method to post request and get response in Xml. After receiveng responce I am trying to show attributes name startswith ("f"), but getting error System.Xml.Linq.XContainer.Element(...) returned null. What am I doing wrong?

postXmlData

public static XmlDocument postXMLData(string xml)
        {
            var request = (HttpWebRequest)WebRequest.Create(Requests.url);
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(xml);
            request.ContentType = &quot;text/xml; encoding=&#39;utf-8&#39;&quot;;
            request.ContentLength = bytes.Length;
            request.Method = &quot;POST&quot;;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                using (var streamReader = new StreamReader(response.GetResponseStream()))
                {
                    var responseText = streamReader.ReadToEnd();
                    var result = new XmlDocument();
                    result.LoadXml(responseText);
                    return result;
                }
            }

            throw new Exception();
        }

Response from Request:

&lt;response result=&quot;0&quot;&gt;
	&lt;prov&gt;
		&lt;getStatus result=&quot;0&quot;&gt;
			&lt;pay date=&quot;2023-02-08T19:44:33+03:00&quot; fatal=&quot;false&quot; id=&quot;8022140013003&quot; result=&quot;0&quot; status=&quot;2&quot; uid=&quot;26775263057008&quot; value-date=&quot;2023-02-08T19:44:40+03:00&quot;&gt;
			&lt;/pay&gt;
		&lt;/getStatus&gt;
	&lt;/prov&gt;
&lt;/response&gt;

To show attributes I am using XElement:

XmlDocument doc = postXMLData(Requests.getStatus(&quot;08022140013003&quot;));
            XElement e = XElement.Load(new XmlNodeReader(doc));
            Console.WriteLine(e); //here ok
           
            IEnumerable&lt;XAttribute&gt; attrs1 = e.Element(&quot;response&quot;).Element(&quot;prov&quot;).Element(&quot;getStatus&quot;).Element(&quot;pay&quot;)?.Attributes().Where(a =&gt; a.Name.LocalName.StartsWith(&quot;f&quot;));
            Console.WriteLine(attrs1);
            Console.ReadKey();

答案1

得分: 0

尝试使用:

```csharp
var attrs1 = e.Element("prov").Element("getStatus").Element("pay").Attributes().Where(a => a.Name.LocalName.StartsWith("f"));

你可以通过以下方式进行验证:

var elements = e.Elements()

这里只有一个元素,它不是 response,而是 prov

编辑:

XmlDocument doc = postXMLData(Requests.getStatus("08022140013003"));
XElement e = XElement.Load(new XmlNodeReader(doc));
//这一行:
var elementsArray = e.Elements().ToArray();
Console.WriteLine(e); //这里没问题

IEnumerable<XAttribute> attrs1 = e.Element("response").Element("prov").Element("getStatus").Element("pay")?.Attributes().Where(a => a.Name.LocalName.StartsWith("f"));
Console.WriteLine(attrs1);
Console.ReadKey();
英文:

Try with:

var attrs1 = e.Element(&quot;prov&quot;).Element(&quot;getStatus&quot;).Element(&quot;pay&quot;).Attributes().Where(a =&gt; a.Name.LocalName.StartsWith(&quot;f&quot;));

You can validate this by doing:

var elements = e.Elements()

There is only 1 element and it is not response but prov.

Edit:

XmlDocument doc = postXMLData(Requests.getStatus(&quot;08022140013003&quot;));
XElement e = XElement.Load(new XmlNodeReader(doc));
//This line:
var elementsArray = e.Elements().ToArray();
Console.WriteLine(e); //here ok
           
IEnumerable&lt;XAttribute&gt; attrs1 = e.Element(&quot;response&quot;).Element(&quot;prov&quot;).Element(&quot;getStatus&quot;).Element(&quot;pay&quot;)?.Attributes().Where(a =&gt; a.Name.LocalName.StartsWith(&quot;f&quot;));
Console.WriteLine(attrs1);
Console.ReadKey();

答案2

得分: 0

在给定的示例中,代码部分保持不变,只需更改所提供的代码片段。你需要将以下部分进行翻译:

Original:

IEnumerable&lt;XAttribute&gt; attrs1 = e.Element(&quot;response&quot;).Element(&quot;prov&quot;).Element(&quot;getStatus&quot;).Element(&quot;pay&quot;)?.Attributes().Where(a =&gt; a.Name.LocalName.StartsWith(&quot;f&quot;));

Revised:

IEnumerable&lt;XAttribute&gt; attrs1 = e.Element(&quot;prov&quot;).Element(&quot;getStatus&quot;).Element(&quot;pay&quot;)?.Attributes().Where(a =&gt; a.Name.LocalName.StartsWith(&quot;f&quot;));
英文:

Given that you do e.g.

        XElement e = XElement.Load(new XmlNodeReader(doc));

the selection should be relative to the response element i.e. instead of

        IEnumerable&lt;XAttribute&gt; attrs1 = e.Element(&quot;response&quot;).Element(&quot;prov&quot;).Element(&quot;getStatus&quot;).Element(&quot;pay&quot;)?.Attributes().Where(a =&gt; a.Name.LocalName.StartsWith(&quot;f&quot;));

you need

        IEnumerable&lt;XAttribute&gt; attrs1 = e.Element(&quot;prov&quot;).Element(&quot;getStatus&quot;).Element(&quot;pay&quot;)?.Attributes().Where(a =&gt; a.Name.LocalName.StartsWith(&quot;f&quot;));

huangapple
  • 本文由 发表于 2023年2月19日 14:46:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定