I want to loop an XML tree and test two conditions and found some issues.
- I can't compare a XElement like an Integer. I've read some other posts (besides this examples) and so far I couldn't figure it out. :S
- In the foreach loop I'm only able to change the first matching element and I would like to do it for all matching XElements. I have tried Any() and All() but wasn't successful.
My XML files: (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>
My XML files: (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>
My code so far:
XDocument atom = XDocument.Load (@"C:\Users\RuiGarcia\Documents\MIB\INESC\C#Tutorial\ProjectC#\Molecule_00\PDBLibary_00\Data\__3Q26.xml");
XDocument protein = XDocument.Load (@"C:\Users\RuiGarcia\Documents\MIB\INESC\C#Tutorial\ProjectC#\Molecule_00\PDBLibary_00\Data\_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:\Users\RuiGarcia\Documents\MIB\INESC\C#Tutorial\ProjectC#\Molecule_00\PDBLibary_00\Data\__3Q26.xml");
P.S. - How can I format correctly the code without separating it in the code sample?
Thanks in advance.