1

Good day!

How can I access this part of XML and print the element name? Please refer to the image below.

enter image description here

I'm now not able to progress with the following codes:

    XmlDataDocument xmldoc = new XmlDataDocument();         
                XmlNode xmlnode;
                FileStream fs = new FileStream(@"D:\Files\20120604\Data_120604-062516_003.xml", FileMode.Open, FileAccess.Read);
                xmldoc.Load(fs);


//MessageBox.Show("YUMDMMATMAS05");
2
  • Read How to Ask. What did you try? did you read the documentation ? Which error do you get ? What is 42? Commented Jun 4, 2012 at 12:12
  • This is what I have done so far and not able to progress on it: XmlDataDocument xmldoc = new XmlDataDocument(); XmlNode xmlnode; FileStream fs = new FileStream(@"D:\Files\20120604\Data_120604-062516_003.xml", FileMode.Open, FileAccess.Read); xmldoc.Load(fs); Commented Jun 4, 2012 at 12:13

3 Answers 3

2

If the behavior you want is to get the root node, then you can get it with xmldoc.DocumentElement.

You can get the name of any XMLElement using the Name property.

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

4 Comments

sorry, but my scenario is I have many XML files and I have to know the name of the very first element for each xml.. For example, XML1 has YUMDMMATMAS05 and XML2 has YUMDMMATMAS05 for the first element. I have no knowldge of the name of the first element
@yonan2236 My solution does not require that you know the name of the first element. I'm saying that you access the first element's name by using xmldoc.DocumentElement.Name in your code above after you call xmldoc.Load().
sorry I did not got it right away, but it is working for me. Thanks Fur
@yonan2236 No problem. Glad to be of help.
0

If you do not need the rest of the document then don't read the entire thing into the memory with XmlDataDocument (obsolete btw) or XmlDocument, but use XmlReader insted (XmlTextReader is also kind of obsolete):

using(var reader = XmlReader.Create(new FileStream(@"D:\Files\20120604\Data_120604-062516_003.xml", FileMode.Open, FileAccess.Read), new XmlReaderSettings { CloseInput = true, DtdProcessing = DtdProcessing.Ignore, IgnoreComments = true, IgnoreProcessingInstructions = true, IgnoreWhitespace = true }))
            {
                XmlNodeType nt;
                do
                {
                    nt = reader.MoveToContent();
                    if(nt == XmlNodeType.Element) {
                        MessageBox.Show(nt.Name);
                        break;
                    }
                }
                while(nt != XmlNodeType.None)
            }

Comments

0

Using Regex

Pattern = @"\?>\s*?<(?<Name>.*?)>.*?</(1)>"

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.