I am unable to find the bug.
3x2m_modified.atoms.xml
<?xml version='1.0' encoding='UTF-8'?>
<pdb>
<atom>
<Protein>3X2M</Protein>
<AtomSerialNo>1</AtomSerialNo>
<AtomSymbol>N</AtomSymbol>
<AtomPosition></AtomPosition>
<AtomNoInTheBranch></AtomNoInTheBranch>
<ConnectedToAtomNoInTheBranch></ConnectedToAtomNoInTheBranch>
<AltLocIndicator>A</AltLocIndicator>
<ResidueName>ALA</ResidueName>
<ChainName>A</ChainName>
<ResidueNo>1</ResidueNo>
<InsertionCode></InsertionCode>
<X>-3.589</X>
<Y>-1.325</Y>
<Z>-13.286</Z>
<Occupancy>0.34</Occupancy>
<TemperatureFactor>5.03</TemperatureFactor>
<SegmentIdentifier></SegmentIdentifier>
<SegmentIdentifierSymbol></SegmentIdentifierSymbol>
<ChargeOfTheAtom></ChargeOfTheAtom>
</atom>
</pdb>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
namespace PdbLib
{
[XmlRoot("pdb")]
public class XmlAtomList
{
[XmlElement("atom")]
public List<XmlAtom> Atoms { get; set; }
}
public class XmlAtom
{
[XmlElement("Protein")]
public string Protein { get; set; }
[XmlElement("AtomSerialNo")]
public int AtomSerialNo { get; set; }
[XmlElement("AtomSymbol")]
public string AtomSymbol { get; set; }
[XmlElement("AtomPosition")]
public string AtomPosition { get; set; }
[XmlElement("AtomNoInTheBranch")]
public int? AtomNoInTheBranch { get; set; }
[XmlElement("ConnectedToAtomNoInTheBranch")]
public int? ConnectedToAtomNoInTheBranch { get; set; }
[XmlElement("AltLocIndicator")]
public string AltLocIndicator { get; set; }
[XmlElement("ResidueName")]
public string ResidueName { get; set; }
[XmlElement("ChainName")]
public string ChainName { get; set; }
[XmlElement("ResidueNo")]
public int ResidueNo { get; set; }
[XmlElement("InsertionCode")]
public string InsertionCode { get; set; }
[XmlElement("X")]
public double X { get; set; }
[XmlElement("Y")]
public double Y { get; set; }
[XmlElement("Z")]
public double Z { get; set; }
[XmlElement("Occupancy")]
public double Occupancy { get; set; }
[XmlElement("TemperatureFactor")]
public double? TemperatureFactor { get; set; }
[XmlElement("SegmentIdentifier")]
public string SegmentIdentifier { get; set; }
[XmlElement("SegmentIdentifierSymbol")]
public string SegmentIdentifierSymbol { get; set; }
[XmlElement("ChargeOfTheAtom")]
public string ChargeOfTheAtom { get; set; }
}
public class XmlADTAtomReader
{
public static Dictionary<int, XmlAtom> Data { get; private set; }
static XmlADTAtomReader()
{
XmlSerializer serializer = new XmlSerializer(typeof(XmlAtomList));
try
{
string xmlString = File.ReadAllText(@"Data\3x2m_modified.atoms.xml");
XmlAtomList xml;
using (StringReader reader = new StringReader(xmlString))
{
using (XmlReader xmlReader = XmlReader.Create(reader))
{
xml = (XmlAtomList)serializer.Deserialize(xmlReader);
}
}
// Manually handle empty elements
foreach (var atom in xml.Atoms)
{
atom.AtomNoInTheBranch = !atom.AtomNoInTheBranch.HasValue ? null : atom.AtomNoInTheBranch;
atom.ConnectedToAtomNoInTheBranch = !atom.ConnectedToAtomNoInTheBranch.HasValue ? null : atom.ConnectedToAtomNoInTheBranch;
}
Data = xml.Atoms.ToDictionary(atom => atom.AtomSerialNo, atom => atom);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using PdbLib;
using System.Linq;
namespace PdbLibTests
{
[TestClass]
public class XmlADTAtomReaderTests
{
[TestInitialize]
public void Setup()
{
// Setup code here, if needed
}
[TestCleanup]
public void Cleanup()
{
// Cleanup code here, if needed
}
[TestMethod]
public void XmlADTAtomReader_Deserialize_ValidXml_Success()
{
var atom = XmlADTAtomReader.Data[0];
// Assert the values
Assert.AreEqual("3X2M", atom.Protein);
Assert.AreEqual(1, atom.AtomSerialNo);
Assert.AreEqual("N", atom.AtomSymbol);
Assert.IsNull(atom.AtomPosition);
Assert.IsNull(atom.AtomNoInTheBranch);
Assert.IsNull(atom.ConnectedToAtomNoInTheBranch);
Assert.AreEqual("A", atom.AltLocIndicator);
Assert.AreEqual("ALA", atom.ResidueName);
Assert.AreEqual("A", atom.ChainName);
Assert.AreEqual(1, atom.ResidueNo);
Assert.AreEqual("", atom.InsertionCode);
Assert.AreEqual(-3.589, atom.X);
Assert.AreEqual(-1.325, atom.Y);
Assert.AreEqual(-13.286, atom.Z);
Assert.AreEqual(0.34, atom.Occupancy);
Assert.AreEqual(5.03, atom.TemperatureFactor);
Assert.IsNull(atom.SegmentIdentifier);
Assert.IsNull(atom.SegmentIdentifierSymbol);
Assert.IsNull(atom.ChargeOfTheAtom);
}
}
}
Test Name: XmlADTAtomReader_Deserialize_ValidXml_Success
Test FullName: PdbLibTests.XmlADTAtomReaderTests.XmlADTAtomReader_Deserialize_ValidXml_Success
Test Source: C:gitProteinAnalysisToolsutXmlADTAtomReader____unit_testUnitTest1.cs : line 27
Test Outcome: Failed
Test Duration: 0:00:00.4733031
Result StackTrace:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXmlAtomList.Read1_NullableOfInt32(Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXmlAtomList.Read4_XmlAtom(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXmlAtomList.Read5_XmlAtomList(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXmlAtomList.Read6_pdb()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
at PdbLib.XmlADTAtomReader..cctor() in C:gitProteinAnalysisToolsPdbLibXmlDataReaderXmlAdtAtomReader.cs:line 88
--- End of inner exception stack trace ---
at PdbLib.XmlADTAtomReader.get_Data()
at PdbLibTests.XmlADTAtomReaderTests.XmlADTAtomReader_Deserialize_ValidXml_Success() in C:gitProteinAnalysisToolsutXmlADTAtomReader____unit_testUnitTest1.cs:line 29
Result Message:
Test method PdbLibTests.XmlADTAtomReaderTests.XmlADTAtomReader_Deserialize_ValidXml_Success threw exception:
System.TypeInitializationException: The type initializer for 'PdbLib.XmlADTAtomReader' threw an exception. ---> System.InvalidOperationException: There is an error in XML document (8, 6). ---> System.FormatException: Input string was not in a correct format.