IXmlLineInfo.LinePosition Property

Gets the current line position.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

int LinePosition { get; }

Property Value

Type: System.Int32
The current line position or 0 if no line information is available (for example, HasLineInfo returns false).

This property is used primarily for error reporting, but can be called at any time. The starting value is 1. Combined with LineNumber, a value of 1,1 indicates the start of a document.


// Create the XML fragment to be parsed.
string xmlFrag =
    @"<book>
             <misc>
                 <style>paperback</style>
                 <pages>240</pages>
             </misc>
         </book>";

// Create the XmlNamespaceManager.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());

// Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

StringBuilder output = new StringBuilder();

// Create the reader.
using (XmlReader reader = XmlReader.Create(new StringReader(xmlFrag), null, context))
{

    IXmlLineInfo lineInfo = ((IXmlLineInfo)reader);
    if (lineInfo.HasLineInfo())
    {

        // Parse the XML and display each node.
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    output.Append(reader.Depth + " " +
                        lineInfo.LineNumber + ", " +
                        lineInfo.LinePosition);
                    output.AppendLine("<" + reader.Name + ">");
                    break;
                case XmlNodeType.Text:
                    output.Append(reader.Depth + " " +
                        lineInfo.LineNumber + ", " +
                        lineInfo.LinePosition);
                    output.AppendLine("  " + reader.Value);
                    break;
                case XmlNodeType.EndElement:
                    output.Append(reader.Depth + " " +
                        lineInfo.LineNumber + ", " +
                        lineInfo.LinePosition);
                    output.AppendLine("</" + reader.Name + ">");
                    break;
            }
        }
    }

    // Close the reader.
}

// Display the output to the TextBlock control
OutputTextBlock.Text = output.ToString();


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show: