Parsing xml file with linq

前端 未结 2 1126
攒了一身酷
攒了一身酷 2021-01-23 17:51

No matter what I try I am unable to parse this xml with linq and get the full-path value:




        
相关标签:
2条回答
  • 2021-01-23 18:40

    You can use the XDocument.Load static method in order to read the full-path value easily, but don't forget to use the element namespace as a prefix:

    var zip = ZipFile.Open(file.FullName, ZipArchiveMode.Read);
    var info = zip.GetEntry("META-INF/container.xml");
    
    var doc = XDocument.Load(info.Open());
    XNamespace ns = "urn:oasis:names:tc:opendocument:xmlns:container";
    var element = doc.Descendants(ns+"rootfile").FirstOrDefault();
    
    if(element != null)
    {
        var fullPath = element.Attribute("full-path").Value;
    }  
    
    0 讨论(0)
  • 2021-01-23 18:50

    You have a namespace, you need to specify it with your element name:

    XNamespace ns = "urn:oasis:names:tc:opendocument:xmlns:container";
    
    var rootFiles = xml.Descendants(ns + "rootfile");
    
    0 讨论(0)
提交回复
热议问题