1

I need to get user id corresponding terminal id. any help. But it's giving error:

The ReadElementContentAsString method is not supported on node type None. Line 1, position 668.

 string strTerminalId = "E";
 XmlDocument xdoc = new XmlDocument();
 xdoc.LoadXml(STRING); //
 string strxml = xdoc.OuterXml;
 string strUserName = "";
 bool Flag = false;

 using (XmlReader reader = XmlReader.Create(new StringReader(strxml)))
 {
     while (reader.Read())
     {
         if (reader.IsStartElement())
         {
             switch (reader.Name)
             {
                 case "Row":
                     reader.Read();
                     if (Flag == false)
                     {
                         reader.ReadToFollowing("TERM-ID");
                         reader.Read();
                         string strTERMID = reader.ReadElementContentAsString().ToString();
                          if (strTERMID == strTerminalId)
                                {
                                    while (reader.ReadToFollowing("NA") && (Flag == false))
                                    {
                                        reader.Read();
                                        string strUser = reader.ReadContentAsString();
                                        if (strUser == "NA")
                                        {
                                            reader.ReadToFollowing("c:value");
                                            reader.Read();
                                            strUserName = reader.ReadContentAsString();
                                            Flag = true;
                                        }
                                    }
                                }
                            }
                            break;
                    }
                }
            }

The content of the XML document is

<GetReferenceTableResponse xmlns='http://tempuri.org/'>
    <GetReferenceTableResult> 
        <Table Name='C' ID='46899' xmlns=''>   
            <Columns>
            <Col ID='7442' Name='TD' Datatype='T' Length='8' AttributeDescription='Terminal ID' IsKey='Y'/>
            <Col ID='7443' Name='D' Datatype='T' Length='50' AttributeDescription='Description' IsKey=' '/>
            <Col ID='7444' Name='U' Datatype='T' Length='8' AttributeDescription='USER-ID' IsKey='' />
            </Columns> 
            <Rows>
                <Row RowsetID=\"1\">
                    <TERM-ID ID='279598'>A</TERM-ID>
                    <DESC-TXT ID='279622'>ASC</DESC-TXT>  
                    <USER-ID ID='279646'>A</USER-ID>
                </Row>
            </Rows> 
        </Table> 
    </GetReferenceTableResult>
</GetReferenceTableResponse>
5
  • what is the error? please edit your question, providing more details, and better give all the code inside the using statement (using (XmlReader reader) Commented Nov 25, 2014 at 4:17
  • 1
    parsing xml document using XmlReader is very inefficient. do you consider using XDocument or XPath? just a few lines of code; or do you have to stick to XmlReader? Commented Nov 25, 2014 at 4:28
  • 1
    @kennyzx - "inefficient" is an interesting term for fastest way of parsing XML using classes that are part of network. Harder to get right than XDocument/XmlDocument - indeed... Commented Nov 25, 2014 at 4:32
  • @AlexeiLevenkov well, can help me with the choice of word, maybe verbose?? when i typed efficient i meant "using less code", not "highest performance". Commented Nov 25, 2014 at 4:37
  • What are your performance considerations? @Alexei is correct that XMLReader is better choice for performance. But if that is not the case you can use LINQ. Much easier and readable constructs. Commented Nov 25, 2014 at 4:43

1 Answer 1

1

ReadToFollowing navigates to the nearest element with a given name and the next Read will go inside that element - straight to the Text. So you would need ReadContentAsString in both cases.

In your case that would work:

using (XmlReader reader = XmlReader.Create(new StringReader(strxml)))
{
    while (reader.Read())
    {
        if (reader.IsStartElement())
        {
            switch (reader.Name)
            {
                case "Row":                     
                    if (!Flag)
                    {
                        reader.ReadToFollowing("TERM-ID");
                        reader.Read();
                        string strTERMID = reader.ReadContentAsString();                         
                        if (strTERMID == strTerminalId && reader.ReadToNextSibling("USER-ID"))
                        {
                            reader.Read();
                            strUserName = reader.ReadContentAsString();
                            Flag = true;
                        }
                    }
                    break;
            }
        }
    }
}

I have removed the first Read just after case "Row": - otherwise you would miss the proper element and as well removed ReadToFollowing("USER-ID") from the while loop - it is okey to go into the element only once.

But as @kennyzx said - it is much simpler to parse the xml using XDoccument.

UPDATE I am not sure about your schema but if it is possible for a Row element to not have User-Id, then with ReadToFollowing it is possible to skip to the next available 'User-ID' element, even if it is not in the same 'Row' element. So it is better to use ReadToNextSibling in the second case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.