I'm not sure if its a result of bad formatting but your code and XML has quite a few issues. The XML is malformed, correctly formatted would be
="1.0"="UTF-8"
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
<field name="Company Name">
<value>ABC</value>
</field>
<field name="Company Address">
<value>Chicago</value>
</field>
<field name="Company phone">
<value>123-456-7894</value>
</field>
<field name="ACSysHP_ASHP_135k_240k_MeasureID">
<value>2635</value>
</field>
</fields>
</xfdf>
And if you are serializing, the correct class structure/notation would be the following (Note: I used
Xml2CSharp.com | Convert your XML Examples into XmlSerializer compatable C# Classes[
^] to convert the XML to c# classes for me)
[XmlRoot(ElementName="field", Namespace="http://ns.adobe.com/xfdf/")]
public class Field {
[XmlElement(ElementName="value", Namespace="http://ns.adobe.com/xfdf/")]
public string Value { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
}
[XmlRoot(ElementName="fields", Namespace="http://ns.adobe.com/xfdf/")]
public class Fields {
[XmlElement(ElementName="field", Namespace="http://ns.adobe.com/xfdf/")]
public List<Field> Field { get; set; }
}
[XmlRoot(ElementName="xfdf", Namespace="http://ns.adobe.com/xfdf/")]
public class Xfdf {
[XmlElement(ElementName="fields", Namespace="http://ns.adobe.com/xfdf/")]
public Fields Fields { get; set; }
[XmlAttribute(AttributeName="xmlns")]
public string Xmlns { get; set; }
[XmlAttribute(AttributeName="space", Namespace="http://www.w3.org/XML/1998/namespace")]
public string Space { get; set; }
}
Then to deserialize your XML to C# objects you'd do
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(xml);
writer.Flush();
stream.Position = 0;
var reader = XmlReader.Create(stream, new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
var myXmlObject = new XmlSerializer(typeof(Xfdf)).Deserialize(reader) as Xfdf;
So once you've desrialized your code, you can loop over it like so
foreach (var item in myXmlObject.Fields.Field)
{
Console.WriteLine("Field Name {0} - Field Value {1}", item.Name, item.Value);
}
Which provides the output
Quote:
Field Name Company Name - Field Value ABC
Field Name Company Address - Field Value Chicago
Field Name Company phone - Field Value 123-456-7894
Field Name ACSysHP_ASHP_135k_240k_MeasureID - Field Value 2635
EDIT:
Altered XML to support new field/property names
="1.0"="UTF-8"
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
<field>
<CompanyName>ABC</CompanyName>
<CompanyPhoneNumber>ABC</CompanyPhoneNumber>
<CompanyAddress>ABC</CompanyAddress>
</field>
<field>
<CompanyName>ABC</CompanyName>
<CompanyPhoneNumber>ABC</CompanyPhoneNumber>
<CompanyAddress>ABC</CompanyAddress>
</field>
<field>
<CompanyName>ABC</CompanyName>
<CompanyPhoneNumber>ABC</CompanyPhoneNumber>
<CompanyAddress>ABC</CompanyAddress>
</field>
<field>
<CompanyName>ABC</CompanyName>
<CompanyPhoneNumber>ABC</CompanyPhoneNumber>
<CompanyAddress>ABC</CompanyAddress>
</field>
</fields>
</xfdf>
Altered Field class for serialization
[XmlRoot(ElementName="field", Namespace="http://ns.adobe.com/xfdf/")]
public class Field {
[XmlElement(ElementName="CompanyName", Namespace="http://ns.adobe.com/xfdf/")]
public string CompanyName { get; set; }
[XmlElement(ElementName="CompanyPhoneNumber", Namespace="http://ns.adobe.com/xfdf/")]
public string CompanyPhoneNumber { get; set; }
[XmlElement(ElementName="CompanyAddress", Namespace="http://ns.adobe.com/xfdf/")]
public string CompanyAddress { get; set; }
}
Which would then allow you to do
foreach (var item in myXmlObject.Fields.Field)
{
Console.WriteLine("CompanyName: {0}", item.CompanyName);
Console.WriteLine("CompanyAddress: {0}", item.CompanyAddress);
Console.WriteLine("CompanyPhoneNumber: {0}", item.CompanyPhoneNumber);
}