Adnan
asked on
How to read XML file attributes...
HI
I have a requirement to to read XML below, and i need help with couple of issues i have.
Here is my XML sample:
I need to read all the attributes in Entety node, and that works fine, but i cant figure out how i can loop thrue all attributes in ChildNodes <Attribute>. I need to read <Attribute Name="xxxxxxx"> and i need to grab the <value> inside CDATA tag.
I need to loop all attributes below, and grab the attrube Name="", and value inside CDATA:
Any help to solve this will be appriciated, and thanks in advance...
I have a requirement to to read XML below, and i need help with couple of issues i have.
Here is my XML sample:
<Entities>
<Entity Id="899" ExternalId="126" LongName="TINE Yog Melon/pasjon 2,5 kg" EntityTypeId="16" EntityTypeName="Produkt" ContainerId="4" ContainerName="Master katalog" OrganizationName="TINE SA" CategoryId="169" CategoryName="1230" CategoryLongName="TINE FruktYoghurt" CategoryPath="120 » 1230" CategoryLongNamePath="" ParentEntityId="169" ParentExternalId="1230" ParentExtensionEntityId="0" ParentExtensionEntityExternalId="" ParentExtensionEntityContainerId="0" ParentExtensionEntityContainerName="" ParentExtensionEntityCategoryId="0" ParentExtensionEntityCategoryPath="" ParentExtensionEntityCategoryLongNamePath="" Locale="no_NO_B" Action="Add">
<Attributes>
<Attribute Id="4041" [u]Name="M3_EAN_Dpak"[/u] LongName="EAN Dpak" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[[u]7038010001260[/u]]]></Value>
</Values>
</Attribute>
<Attribute Id="4051" Name="M3_Artikkelnr" LongName="Artikkelnr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[126]]></Value>
</Values>
</Attribute>
<Attribute Id="4076" Name="M3_Navn" LongName="M3 Navn" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2,5 KG YOG. MELON/PASJON SPANN]]></Value>
</Values>
</Attribute>
<Attribute Id="4200" Name="TS_EPD_varenr" LongName="EPD varenr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4003" AttributeParentName="Pricat" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2104115]]></Value>
</Values>
</Attribute>
</Attributes>
<Hierarchies></Hierarchies>
<Relationships />
<Extensions></Extensions>
</Entity>
</Entities>
I need to read all the attributes in Entety node, and that works fine, but i cant figure out how i can loop thrue all attributes in ChildNodes <Attribute>. I need to read <Attribute Name="xxxxxxx"> and i need to grab the <value> inside CDATA tag.
I need to loop all attributes below, and grab the attrube Name="", and value inside CDATA:
<Attribute Id="4041" [u]Name="M3_EAN_Dpak"[/u] LongName="EAN Dpak" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[[u]7038010001260[/u]]]></Value>
</Values>
</Attribute>
<Attribute Id="4051" Name="M3_Artikkelnr" LongName="Artikkelnr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[126]]></Value>
</Values>
</Attribute>
<Attribute Id="4076" Name="M3_Navn" LongName="M3 Navn" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2,5 KG YOG. MELON/PASJON SPANN]]></Value>
</Values>
</Attribute>
<Attribute Id="4200" Name="TS_EPD_varenr" LongName="EPD varenr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4003" AttributeParentName="Pricat" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2104115]]></Value>
</Values>
</Attribute>
Any help to solve this will be appriciated, and thanks in advance...
What classes are you using to read the Attributes anode and Child nodes. Are you using Linq to XML?
ASKER
Iam using XMLDocument, se code below:
private static void readXMLFile(SPODataContext sp, string filePath)
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNodeList elemList = doc.GetElementsByTagName("Entity");
for (int i = 0; i < elemList.Count; i++)
{
XmlNode elm = elemList[i];
getEntity(elm);
}
}
private static void getEntity(XmlNode elm)
{
for (int i = 0; i < elm.Attributes.Count; i++)
{
XmlAttribute entetyAttri = elm.Attributes[i];
switch (entetyAttri.Name)
{
case "Id":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ExternalId":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "LongName":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "EntityTypeId":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "EntityTypeName":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ContainerId":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ContainerName":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "OrganizationName":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "CategoryId":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "CategoryName":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "CategoryLongName":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "CategoryPath":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "CategoryLongNamePath":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ParentEntityId":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ParentExternalId":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ParentExtensionEntityId":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ParentExtensionEntityExternalId":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ParentExtensionEntityContainerId":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ParentExtensionEntityContainerName":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ParentExtensionEntityCategoryId":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ParentExtensionEntityCategoryPath":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "ParentExtensionEntityCategoryLongNamePath":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
case "Locale":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
default:
Console.WriteLine("Default case");
break;
}
}
XmlNodeList celm = elm.ChildNodes;
getAttributes(celm);
}
public static int count;
private static void getAttributes(XmlNodeList celm)
{
count = celm.Count;
for (int i = 0; i <= celm.Count - 1; i++)
{
var childNd = celm[i].Name;
if (childNd == "Attributes")
{
for (int ci = 0; ci < celm.Count; ci++)
{
XmlNode clm = celm[ci].ChildNodes[ci];
if(clm != null)
{
for (int att = 0; att < clm.Attributes.Count; att++)
{
if (clm.Attributes[att].NodeType.ToString() == "Attribute")
{
XmlAttribute entetyAttri = clm.Attributes[att];
switch (entetyAttri.Name)
{
case "Name":
Console.WriteLine(entetyAttri.Name);
Console.WriteLine(entetyAttri.Value);
break;
default:
Console.WriteLine("Default case");
break;
}
}
}
}
}
}
count++;
}
}
ASKER
Here is the XML full sample, with only one Entity, but there gona be hundreds of them, and attrube can be 1 or more...:
<?xml version="1.0" encoding="utf-8"?>
<Data Schema="RSXML4.1" xmlns="http://schemas.riversand.com/mdmcenter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.riversand.com/mdmcenter/RSXml4.1.xsd">
<Entities>
<Entity Id="899" ExternalId="126" LongName="TINE Yog Melon/pasjon 2,5 kg" EntityTypeId="16" EntityTypeName="Produkt" ContainerId="4" ContainerName="Master katalog" OrganizationName="TINE SA" CategoryId="169" CategoryName="1230" CategoryLongName="TINE FruktYoghurt" CategoryPath="120 » 1230" CategoryLongNamePath="" ParentEntityId="169" ParentExternalId="1230" ParentExtensionEntityId="0" ParentExtensionEntityExternalId="" ParentExtensionEntityContainerId="0" ParentExtensionEntityContainerName="" ParentExtensionEntityCategoryId="0" ParentExtensionEntityCategoryPath="" ParentExtensionEntityCategoryLongNamePath="" Locale="no_NO_B" Action="Add">
<Attributes>
<Attribute Id="4041" Name="M3_EAN_Dpak" LongName="EAN Dpak" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[7038010001260]]></Value>
</Values>
</Attribute>
<Attribute Id="4051" Name="M3_Artikkelnr" LongName="Artikkelnr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[126]]></Value>
</Values>
</Attribute>
<Attribute Id="4076" Name="M3_Navn" LongName="M3 Navn" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2,5 KG YOG. MELON/PASJON SPANN]]></Value>
</Values>
</Attribute>
<Attribute Id="4200" Name="TS_EPD_varenr" LongName="EPD varenr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4003" AttributeParentName="Pricat" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2104115]]></Value>
</Values>
</Attribute>
</Attributes>
<Hierarchies></Hierarchies>
<Relationships />
<Extensions></Extensions>
</Entity>
</Entities>
</Data>
Can you use XDocument object instead of XMLDocument objects?
ASKER
Yes why not, but i havent used XDocument before, any sample you can show for this XML?
ASKER
I tried it this way, but its returning null:
private static void readXMLFile(SPODataContext sp, string filePath)
{
try
{
XDocument xdoc = XDocument.Load(filePath);
var groupElements = from el in xdoc.Descendants().Elements("Entity") select el;
// Console.WriteLine("");
}
catch(Exception exp)
{
Console.WriteLine(exp);
}
}
ASKER
Why iam getting result "null" when i use XDocuemnt class:
"Enumeration yielded no results"
XDocument xdoc = XDocument.Load(filePath);
//var groupElements = from el in xdoc.Descendants().Elements("Entity") select el;
var query = xdoc.Descendants("Entities");
"Enumeration yielded no results"
It is most likely due to the xml namespace in the root node.
What is the Encoding of the full XML document you posted? There seems to be an invalid character on line 4 character position 288, a double greater then sign as one character hex value BB.
What is the Encoding of the full XML document you posted? There seems to be an invalid character on line 4 character position 288, a double greater then sign as one character hex value BB.
ASKER
Sorry i cant see the namespace, it is Encoding UTF-8
XML:
XML:
<?xml version="1.0" encoding="utf-8"?>
<Data Schema="RSXML4.1" xmlns="http://schemas.riversand.com/mdmcenter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.riversand.com/mdmcenter/RSXml4.1.xsd">
<Entities>
<Entity Id="899" ExternalId="126" LongName="TINE Yog Melon/pasjon 2,5 kg" EntityTypeId="16" EntityTypeName="Produkt" ContainerId="4" ContainerName="Master katalog" OrganizationName="TINE SA" CategoryId="169" CategoryName="1230" CategoryLongName="TINE FruktYoghurt" CategoryPath="120 » 1230" CategoryLongNamePath="" ParentEntityId="169" ParentExternalId="1230" ParentExtensionEntityId="0" ParentExtensionEntityExternalId="" ParentExtensionEntityContainerId="0" ParentExtensionEntityContainerName="" ParentExtensionEntityCategoryId="0" ParentExtensionEntityCategoryPath="" ParentExtensionEntityCategoryLongNamePath="" Locale="no_NO_B" Action="Add">
<Attributes>
<Attribute Id="4041" Name="M3_EAN_Dpak" LongName="EAN Dpak" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[7038010001260]]></Value>
</Values>
</Attribute>
<Attribute Id="4051" Name="M3_Artikkelnr" LongName="Artikkelnr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[126]]></Value>
</Values>
</Attribute>
<Attribute Id="4076" Name="M3_Navn" LongName="M3 Navn" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2,5 KG YOG. MELON/PASJON SPANN]]></Value>
</Values>
</Attribute>
<Attribute Id="4200" Name="TS_EPD_varenr" LongName="EPD varenr" InstanceRefId="-1" Sequence="-1" AttributeParentId="4003" AttributeParentName="Pricat" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[2104115]]></Value>
</Values>
</Attribute>
</Attributes>
<Hierarchies></Hierarchies>
<Relationships />
<Extensions></Extensions>
</Entity>
</Entities>
</Data>
Fernando Soto is talking about
And I'm pretty sure he's right. If you remove namespaces from your xml you'll be able to read elements.
Or you will need to use XNamespace Class.
xmlns="http://schemas.riversand.com/mdmcenter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
in the Data root.And I'm pretty sure he's right. If you remove namespaces from your xml you'll be able to read elements.
Or you will need to use XNamespace Class.
Try something like:
class Program
{
static void Main(string[] args)
{
readXMLFile("data.xml");
Console.ReadLine();
}
private static void readXMLFile(string filePath)
{
try
{
XDocument xdoc = XDocument.Load(filePath);
XNamespace nsSys = "http://schemas.riversand.com/mdmcenter";
var entities = from el in xdoc.Descendants(nsSys + "Entity")
select new
{
Id=el.Attribute("Id").Value,
LongName=el.Attribute("LongName").Value
};
foreach (var p in entities)
Console.WriteLine(p.ToString());
Console.WriteLine("");
}
catch (Exception exp)
{
Console.WriteLine(exp);
}
}
}
When you did this
XDocument xdoc = XDocument.Load(filePath);
//var groupElements = from el in xdoc.Descendants().Elements("Entity") select el;
var query = xdoc.Descendants("Entities");
did you not get an error? If not what XML document did you use?
ASKER
I managed to read the node <Entity>.
This code sample worked:
This code sample worked:
private static void readXMLFile(SPODataContext sp, string filePath)
{
try
{
XDocument xdoc = XDocument.Load(filePath);
var query = from d in xdoc.Descendants()
where d.Name.LocalName == "Entity"
select d;
Console.WriteLine(query);
}
catch (Exception exp)
{
Console.WriteLine(exp);
}
}
ASKER
Perfect anarki_jimbel, your sample worked as charm, but how can i grab the value from choldnodes:
f.exp
I need to grab the <Attribute> Name="M3_EAN_Dpak"
And value [CDATA[7038010001260]]
it can be one or more Attributes in one Entetity.
f.exp
<Attribute Id="4041" Name="M3_EAN_Dpak" LongName="EAN Dpak" InstanceRefId="-1" Sequence="-1" AttributeParentId="4001" AttributeParentName="M3" AttributeType="Simple" AttributeDataType="String" Locale="no_NO_B" Action="Add">
<Values>
<Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="no_NO_B" Action="Add"><![CDATA[7038010001260]]></Value>
</Values>
</Attribute>
I need to grab the <Attribute> Name="M3_EAN_Dpak"
And value [CDATA[7038010001260]]
it can be one or more Attributes in one Entetity.
ASKER
to be more correct i need to grab Name and CDATA for each Attribute...
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Hi,
This did the job for me, now i understand bit more how to use XDocument class, thanks alot for your help....
This did the job for me, now i understand bit more how to use XDocument class, thanks alot for your help....