如何获取具有可选值minOccurs = 0的xml元素

时间:2018-05-18 12:20:57

标签: c# linq

我需要过滤具有可选值minOccurs = 0的Xml元素 这就是我尝试过的事情

    var XMLDocumentMaster = XDocument.Load(@"D:\Stephen\Documents\Schema\AgileFlow_Import.xsd");
    XMLDocumentM = XMLDocumentMaster();
    var prefix = XMLDocumentMaster.Root.GetNamespaceOfPrefix("xs");
    // get Vehicle
    var vehicle = XMLDocumentMaster.Root.Element(prefix + "element");
    // get sequence for Ford
    var sections = vehicle.Element(prefix + "complexType")
                        .Element(prefix + "sequence")
                        // the Ford element
                        .Element(prefix + "element")
                        .Element(prefix + "complexType")
                        .Element(prefix + "sequence")
                        // elements
                        .Elements(prefix + "element").ToList();

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

dat01_characterization2<-data.frame(usubjid = as.factor(sample(10)), col2 = letters[1:10], col3 = letters[1:10])
dat02_consent2<-data.frame(usubjid = as.factor(sample(10)), col3 = letters[1:10], col4 = letters[1:10])
dat03_psqi2<-data.frame(usubjid = as.factor(sample(10)), col5 = letters[1:10], col3 = letters[1:10])

l3<-mget(ls(pattern="dat0"))

out<-l3[[1]]
for(i in 2:length(l3)){
  yname<-substr(names(l3[i]),regexpr("_",names(l3[i]))+1,nchar(names(l3[i])))
  out<-merge(out,l3[[i]],by=c("usubjid"),all=TRUE)
  colnames(out)<-gsub("\\.x","",names(out))
  colnames(out)<-gsub('\\.y',paste0("\\.",yname),names(out))
}

答案 1 :(得分:0)

    private void button2_Click(object sender, EventArgs e)
    {
        List<string> objdec = new List<string>();
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"D:\Stephen\Documents\Schema\AgileFlow_Import.xsd");
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        XmlNodeList nodes = xmlDoc.SelectNodes("//xs:element[@name='IR_DocumentMaster']//xs:complexType//xs:all//xs:element", nsmgr);
        try
        {
            objdec = nodes.Cast<XmlNode>().Where(node =>  node.Attributes.GetNamedItem("minOccurs") != null && node.Attributes.GetNamedItem("minOccurs").Value == "0")
                           .Select(x => x.Attributes["name"].Value).ToList();