1

I'm writing a program that reads an XML (Element only) and stores the value into database.

But there are such no methods in XmlReader Class to get the name/value of Parent Node of a child node. Is there any workaround or should i use any other parser.

4
  • Have you tried XmlSerialization ? Commented Nov 12, 2014 at 10:29
  • 2
    Do you need to use XmlReader because the XML is so large? If not, I'd go straight for LINQ to XML. Commented Nov 12, 2014 at 10:31
  • @SandySands: There's no indication that this is XML representing a serialized object. For general XML documents, serialization isn't a good call. Commented Nov 12, 2014 at 10:31
  • The objective of my program is to read the xml (without DTD/schema) create tables dynamically, and store the values into tables. Currently i'm creating xsd (using xsd.exe) from xml, using the xsd i'm manually creating tables and using my program storing the values from xml into tables. What according to you is the best approach to achieve this? Commented Nov 12, 2014 at 10:38

4 Answers 4

4

As XmlReader provides forward-only access, it is not possible at least without reading the document more than once. Why not to use Linq to XML?

var xml = XElement.Load(xmlReader);
var element = xml.Descendants("someElement").First();
var parent = element.Parent;
Sign up to request clarification or add additional context in comments.

Comments

2

Maybe check the XmlReader.ReadSubtree method. It creates a NEW XmlReader so that you can derive the children nodes' information from it without moving the original XmlReader to a much to deep level ...

Comments

1

As MSDN says about XmlReader:

Represents a reader that provides fast, noncached, forward-only access to XML data

Сonsidering that, XMLReader is better for processing large XML. If that is your case, you need to store info about parent node before you move to process nested nodes. But if you work with small XML, tha better choise to process nodes is LINQ to XML (as @Juriewicz Bartek has advised to you), that make related navigation easier.

Comments

0

If you are on the .Net Framework and you're not bothered to be future-proof and so stay on .NET 4.8, you could use reflection:

    static void Main(string[] args)
    {
        var xml = @"<document><node1>my text</node1>/document>";
        var reader = XmlReader.Create(new StringReader(xml));
        var nodesInfo = reader.GetType().GetField("nodes", BindingFlags.NonPublic | BindingFlags.Instance| BindingFlags.FlattenHierarchy);
        dynamic nodes = nodesInfo.GetValue(reader);
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    {
                        switch (reader.Depth)
                        {
                            case 1:
                                var node = nodes[reader.Depth - 1];
                                var localNameInfo = node.GetType().GetField("localName", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
                                var localName = (string) localNameInfo.GetValue(node);
                                Console.WriteLine("Parent node = " + localName);
                                return;
                        }
                    }
                    break;
            }
        }
    }

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.