我想循环XML树并测试两个条件并发现一些问题。
我的XML文件:(对于蛋白质)
<?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文件:(对于原子)
<?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:\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");
&#13;
P.S。 - 如何在不将代码示例中的代码分开的情况下正确格式化代码?
提前致谢。
答案 0 :(得分:0)
您需要第二个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 :(得分:0)
我在这里看到几个问题:
foreach
循环而不是FirstOrDefault()
。(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"));
}
}