how to read a xml?

mc 3,481 Reputation points
2021-11-28T09:00:52.4+00:00
<package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="bookid">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:identifier id="bookid">easypub-ed863743</dc:identifier>
<meta name="cover" content="cover-image"/>
</metadata>
</package>

I can get the metadata node how to get the dc:identifier node?

metadataNode.SelectSingleNode("/dc:identifier) is null

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,981 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,266 Reputation points Microsoft Vendor
    2021-11-30T02:33:26.38+00:00

    @mc , Based on my test, you could try the following code to get identifier from metadata node.

      private void button1_Click(object sender, EventArgs e)  
            {  
                string path = "D:\\test.xml";  
                XNamespace xn1 = "http://www.idpf.org/2007/opf";  
                XDocument doc = XDocument.Load(path);  
                XNamespace xn2 = "http://purl.org/dc/elements/1.1/";  
                var result = doc.Descendants(xn1 + "metadata");  
      
                foreach (var item in result.Descendants(xn2+ "identifier"))  
                {  
                    textBox1.Text = item.Value;  
                    textBox2.Text = item.Attribute("id").Value;  
                    //Console.WriteLine(item.Value);  
                    //Console.WriteLine(item.Attribute("id").Value);  
                }  
      
      
            }  
    

    Also, I think another method will be simpler if we don't consider that we need to get identifier from metadata node.

     private void button1_Click(object sender, EventArgs e)  
            {  
                string path = "D:\\test.xml";  
                XDocument doc = XDocument.Load(path);  
                XNamespace xn2 = "http://purl.org/dc/elements/1.1/";  
                
                var item= doc.Descendants(xn2 + "identifier").FirstOrDefault();  
                textBox1.Text = item.Value;  
                textBox2.Text = item.Attribute("id").Value;  
      
      
            }  
    

    The both methods can get the following result:

    153551-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Nasreen Akter 10,716 Reputation points
    2021-11-28T11:50:00.227+00:00

    Hi @mc ,

    Please check the the link below:

    https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectsinglenode?view=net-6.0

    Hope this will help. Thanks!


  2. P a u l 10,306 Reputation points
    2021-11-28T15:06:06.743+00:00

    You just need to make sure you add namespaces for elements you're selecting using XmlNamespaceManager like the example that nasreen linked to:

    using System.Xml;
    
    string xml = @"
    <package version=""2.0"" xmlns=""http://www.idpf.org/2007/opf"" unique-identifier=""bookid"">
        <metadata xmlns:dc=""http://purl.org/dc/elements/1.1/"" xmlns:opf=""http://www.idpf.org/2007/opf"">
            <dc:identifier id=""bookid"">easypub-ed863743</dc:identifier>
            <meta name=""cover"" content=""cover-image""/>
        </metadata>
    </package>
    ".Trim();
    
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    
    XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
    nsMgr.AddNamespace("df", "http://www.idpf.org/2007/opf"); // Default namespace to select <metadata>
    nsMgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
    
    XmlElement root = doc.DocumentElement;
    XmlNode identifier = root.SelectSingleNode("//dc:identifier", nsMgr);
    XmlNode metadata = root.SelectSingleNode("//df:metadata", nsMgr);
    
    Console.WriteLine(identifier.InnerText);