質問

I have a file that I'm opening from the Isolated storage. I'm getting an XML that is string type. I need to parse the XML to get specific field content but having some problems regarding it.

I've tried to use XDocument.Parse and got the XML, then I used Descendants and wrote the name of the node I needed , but when debugging it - I see that the returned value is NULL.

Can someone please tell me what am I doing wrong?

IsolatedStorageFileStream file = storage.OpenFile(txtFilePath, FileMode.Open, FileAccess.Read);
                using (StreamReader reader = new StreamReader(file))
                   {
                     //message += reader.ReadToEnd();
                      message = reader.ReadToEnd();
                   }
                  var doc = XDocument.Parse(message);
                  var res = doc.Descendants("value").Select(o => o.Value).ToArray();
                  // res is NULL
                  var x = res[0];
                  message = x;

This is the XML document, The only content I need is the ine under the node <value> (third line)

    <xsi:document xmlns="@link" xmlns:xsi="@link" xsi:schemaLocation="@link" version="1.0">
        <xsi:field left="51" top="235" right="224" bottom="280" type="text">
        <xsi:value encoding="utf-16">| amount: 152.467</xsi:value> --- THE LINE I NEED
        <xsi:line left="52" top="245" right="179" bottom="267">
        <xsi:char left="52" top="245" right="55" bottom="267" confidence="93">|</xsi:char>
        <xsi:char left="56" top="245" right="78" bottom="267" confidence="100"></xsi:char>
        <xsi:char left="79" top="254" right="84" bottom="261" confidence="43">a</xsi:char>
        <xsi:char left="86" top="254" right="96" bottom="261" confidence="100">m</xsi:char>
        <xsi:char left="98" top="254" right="105" bottom="261" confidence="94">o</xsi:char>
        <xsi:char left="106" top="254" right="112" bottom="261" confidence="31">u</xsi:char>
        <xsi:char left="114" top="254" right="120" bottom="261" confidence="34">n</xsi:char>
        <xsi:char left="121" top="252" right="126" bottom="261" confidence="59">t</xsi:char>
        <xsi:char left="127" top="254" right="129" bottom="261" confidence="76">:</xsi:char>
        <xsi:char left="130" top="252" right="133" bottom="261" confidence="100"></xsi:char>
        <xsi:char left="134" top="252" right="140" bottom="261" confidence="100">1</xsi:char>
         <xsi:char left="141" top="252" right="147" bottom="261" confidence="100">5</xsi:char>
        <xsi:char left="148" top="252" right="154" bottom="261" confidence="55">2</xsi:char>
        <xsi:char left="155" top="259" right="157" bottom="261" confidence="100" suspicious="true">.</xsi:char>
        <xsi:char left="158" top="252" right="165" bottom="261" confidence="71">4</xsi:char>
        <xsi:char left="166" top="252" right="172" bottom="261" confidence="40">6</xsi:char>
        <xsi:char left="173" top="252" right="179" bottom="261" confidence="100">7</xsi:char>
       </xsi:line>
     </xsi:field>
   </xsi:document>

Thanks a lot,

役に立ちましたか?

解決

Since your XML uses namespaces, you need to specify the namespace to the Descendants method. The xsi namespace is defined by the attribute xmlns:xsi="@link" at the beginning of your XML document.

So in your case, the code would be:

var doc = XDocument.Parse(message);
var res = doc.Descendants(XName.Get("value", "@link")).Select(o => o.Value).ToArray();
var x = res[0];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top