基于Condition循环遍历两个xelement



我想循环一个XML树并测试两个条件,发现了一些问题。

  1. 我不能像比较Integer那样比较XElement。我读了一些其他的帖子(除了这个例子),到目前为止我还不能弄清楚。: S

Link_1

Link_2

  • 在foreach循环中,我只能更改第一个匹配元素,我想对所有匹配的xelement都这样做。我已经尝试了Any()和All(),但没有成功。
  • 我的XML文件:(for protein)

    <?xml version="1.0" encoding="utf-8"?>
    <ProteinStructure>
      <SecondaryStructure>
        <StName>HELX_P1</StName>
        <StType>alphaHelix</StType>
        <AmAcidStart>1</AmAcidStart>
        <AmAcidEnd>2</AmAcidEnd>
      </SecondaryStructure>
      <SecondaryStructure>
        <StName>HELX_P2</StName>
        <StType>alphaHelix</StType>
        <AmAcidStart>43</AmAcidStart>
        <AmAcidEnd>53</AmAcidEnd>
      </SecondaryStructure>
    

    我的XML文件:(for atom)

    <?xml version="1.0" encoding="utf-8"?>
    <Molecule>
      <Atom>
        <AtNum>1</AtNum>
        <AmAcSeq>2</AmAcSeq>
        <AtType>N</AtType>
        <StType>turn</StType>
      </Atom>
      <Atom>
        <AtNum>2</AtNum>
        <AmAcSeq>2</AmAcSeq>
        <AtType>CA</AtType>
        <StType>turn</StType>
      </Atom>
      <Atom>
        <AtNum>2</AtNum>
        <AmAcSeq>2</AmAcSeq>
        <AtType>C</AtType>
        <StType>turn</StType>
      </Atom>
      <Atom>
        <AtNum>1</AtNum>
        <AmAcSeq>3</AmAcSeq>
        <AtType>N</AtType>
        <StType>turn</StType>
      </Atom>
    

    我的代码:

    			XDocument atom = XDocument.Load (@"C:UsersRuiGarciaDocumentsMIBINESCC#TutorialProjectC#Molecule_00PDBLibary_00Data__3Q26.xml");
    			XDocument protein = XDocument.Load (@"C:UsersRuiGarciaDocumentsMIBINESCC#TutorialProjectC#Molecule_00PDBLibary_00Data_3Q26.xml");
    			//Change secondary structure tag type "turn" to helixAlpha or betaSheet
    			foreach (XElement amAc in protein.Descendants ("SecondaryStructure"))
    			{
    				atom.Element ("Molecule")
    					.Elements ("Atom")
    					.Where (x => (int?)x.Element("AmAcSeq") >= (int?)amAc.Element("AmAcidStart") && x => (int?)ToInt16.x.Element ("AmAcSeq") <= (int?)amAc.Element("AmAcidEnd"))
    					.Select (x => x.Element("StType")).FirstOrDefault().SetValue(amAc.Element("StType"));
    //				Console.WriteLine (amAc.Element("AmAcidStart").Value);
    //					.Where (x => Convert.ToInt16(x.Element("AmAcSeq").Value) <= Convert.ToInt16(amAc.Element("AmAcidStart").Value) && x => Convert.ToInt16(x.Element ("AmAcSeq").Value) >= Convert.ToInt16(amAc.Element("AmAcidStart")))
    //					.Where (x => (int?)x.Element("AmAcSeq") >= (int?)amAc.Element("AmAcidStart") && x => (int?)ToInt16.x.Element ("AmAcSeq") <= (int?)amAc.Element("AmAcidStart"))
    			}
    			atom.Save(@"C:UsersRuiGarciaDocumentsMIBINESCC#TutorialProjectC#Molecule_00PDBLibary_00Data__3Q26.xml");

    注:-我怎么能正确地格式化代码,而不分离它在代码样本?

    您需要第二个foreach来遍历结果。我删除了一些值,并在下面添加了一个这样做的示例。

    XDocument atom = XDocument.Load (@"...__3Q26.xml");
    XDocument protein = XDocument.Load (@"..._3Q26.xml");
    //Change secondary structure tag type "turn" to helixAlpha or betaSheet
    foreach (XElement amAc in protein.Descendants ("SecondaryStructure"))
    {
        int? start = (int?)amAc.Element("AmAcidStart");
        int? end = (int?)amAc.Element("AmAcidEnd");
        string stType = (string)amAc.Element("StType");
        IEnumerable<XElement> atoms = atom.Element("Molecule").Elements("Atom");
        // Here we are iterating again in each matching result
        foreach (XElement atomElement in atoms.Where(elem => (int?)elem.Element("AmAcSeq") >= start && (int?)elem.Element("AmAcSeq") <= end))
        {
            atomElement.SetValue(stType);
        }
    }
    

    我发现这里有几个问题:

    1. 你需要使用嵌套的foreach循环而不是FirstOrDefault()
    2. 你的条件(int?)ToInt16.x.Element ("AmAcSeq") <= (int?)amAc.Element("AmAcidStart")甚至不能编译,应该几乎肯定是(int?)x.Element ("AmAcSeq") <= (int?)amAc.Element("AmAcidEnd"))

    :

            foreach (XElement secondaryStructure in protein.Descendants ("SecondaryStructure"))
            {
                foreach (var atomStType in atom.Element ("Molecule")
                    .Elements ("Atom")
                    .Where(a => (int?)a.Element("AmAcSeq") >= (int?)secondaryStructure.Element("AmAcidStart") && (int?)a.Element("AmAcSeq") <= (int?)secondaryStructure.Element("AmAcidEnd"))
                    .Select (a => a.Element("StType")))
                {
                    atomStType.SetValue((string)secondaryStructure.Element("StType"));
                }
            }
    

    最新更新