Jayesh Acharya
asked on
get xml name and value from the file in C#
I have an xml file with the following format:
<?xml version="1.0" ?>
- <root>
<SalesPartNoIndex>0</Sales PartNoInde x>
<SalesPartDescriptionIndex >1</SalesP artDescrip tionIndex>
<CustomerIDIndex>2</Custom erIDIndex>
<BasePriceIndex>3</BasePri ceIndex>
<SalesPriceIndex>4</SalesP riceIndex>
<NetPriceIndex>5</NetPrice Index>
<QuantityAvlIndex>6</Quant ityAvlInde x>
<PlannableQtyIndex>7</Plan nableQtyIn dex>
<StatusIndex>8</StatusInde x>
<DiscountTypeIndex>10</Dis countTypeI ndex>
<DiscountPercIndex>11</Dis countPercI ndex>
<SubstPartIndex>12</SubstP artIndex>
<AuthorizedIndex>13</Autho rizedIndex >
<CatalogTypeIndex>14</Cata logTypeInd ex>
<ProductFamIndex>15</Produ ctFamIndex >
<ProductGrpIndex>16</Produ ctGrpIndex >
<ReservationCtrlIndex>17</ Reservatio nCtrlIndex >
<SalesPartNoWidth>100</Sal esPartNoWi dth>
<SalesPartDescriptionWidth >100</Sale sPartDescr iptionWidt h>
<CustomerIDWidth>100</Cust omerIDWidt h>
<BasePriceWidth>100</BaseP riceWidth>
<SalesPriceWidth>100</Sale sPriceWidt h>
<NetPriceWidth>100</NetPri ceWidth>
<QuantityAvlWidth>100</Qua ntityAvlWi dth>
<PlannableQtyWidth>100</Pl annableQty Width>
<StatusWidth>100</StatusWi dth>
<DiscountTypeWidth>100</Di scountType Width>
<DiscountPercWidth>100</Di scountPerc Width>
<SubstPartWidth>100</Subst PartWidth>
<AuthorizedWidth>100</Auth orizedWidt h>
<CatalogTypeWidth>100</Cat alogTypeWi dth>
<ProductFamWidth>100</Prod uctFamWidt h>
<ProductGrpWidth>100</Prod uctGrpWidt h>
<ReservationCtrlWidth>100< /Reservati onCtrlWidt h>
<SATPWidth>100</SATPWidth>
<SATPDescriptionWidth>100< /SATPDescr iptionWidt h>
<CatalogCodeWidth>100</Cat alogCodeWi dth>
<SATPIndex>18</SATPIndex>
<SATPDescriptionIndex>19</ SATPDescri ptionIndex >
<CatalogCodeIndex>20</Cata logCodeInd ex>
</root>
I am trying to read each name and get its value
for example
<SATPDescriptionIndex>19</ SATPDescri ptionIndex >
name = SATPDescriptionIndex
value =19
columnname = SATPDescription
columntype = Index
<SATPDescriptionWidth>100< /SATPDescr iptionWidt h>
name = SATPDescriptionWidth
value =100
columnname = SATPDescription
columntype = Width
I am trying to place these values into a Dictionary
if (columntype == "Width")
{ WidthDictionary.Add(column name,value ); }
if (columntype == "Index")
{ IndexDictionary.Add(column name,value ); }
thanks
<?xml version="1.0" ?>
- <root>
<SalesPartNoIndex>0</Sales
<SalesPartDescriptionIndex
<CustomerIDIndex>2</Custom
<BasePriceIndex>3</BasePri
<SalesPriceIndex>4</SalesP
<NetPriceIndex>5</NetPrice
<QuantityAvlIndex>6</Quant
<PlannableQtyIndex>7</Plan
<StatusIndex>8</StatusInde
<DiscountTypeIndex>10</Dis
<DiscountPercIndex>11</Dis
<SubstPartIndex>12</SubstP
<AuthorizedIndex>13</Autho
<CatalogTypeIndex>14</Cata
<ProductFamIndex>15</Produ
<ProductGrpIndex>16</Produ
<ReservationCtrlIndex>17</
<SalesPartNoWidth>100</Sal
<SalesPartDescriptionWidth
<CustomerIDWidth>100</Cust
<BasePriceWidth>100</BaseP
<SalesPriceWidth>100</Sale
<NetPriceWidth>100</NetPri
<QuantityAvlWidth>100</Qua
<PlannableQtyWidth>100</Pl
<StatusWidth>100</StatusWi
<DiscountTypeWidth>100</Di
<DiscountPercWidth>100</Di
<SubstPartWidth>100</Subst
<AuthorizedWidth>100</Auth
<CatalogTypeWidth>100</Cat
<ProductFamWidth>100</Prod
<ProductGrpWidth>100</Prod
<ReservationCtrlWidth>100<
<SATPWidth>100</SATPWidth>
<SATPDescriptionWidth>100<
<CatalogCodeWidth>100</Cat
<SATPIndex>18</SATPIndex>
<SATPDescriptionIndex>19</
<CatalogCodeIndex>20</Cata
</root>
I am trying to read each name and get its value
for example
<SATPDescriptionIndex>19</
name = SATPDescriptionIndex
value =19
columnname = SATPDescription
columntype = Index
<SATPDescriptionWidth>100<
name = SATPDescriptionWidth
value =100
columnname = SATPDescription
columntype = Width
I am trying to place these values into a Dictionary
if (columntype == "Width")
{ WidthDictionary.Add(column
if (columntype == "Index")
{ IndexDictionary.Add(column
thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi GoJoe
I am having an issue trying to compile the code, what DLL do I need to use XDocumnet and myxml
on the code line shown below both Xdocument and myxml do not exist..
var resultAllInOne = from x in XDocument.Parse(myxml).Roo t.Elements ()
I am having an issue trying to compile the code, what DLL do I need to use XDocumnet and myxml
on the code line shown below both Xdocument and myxml do not exist..
var resultAllInOne = from x in XDocument.Parse(myxml).Roo
ASKER
ok found the library its system.xml.linq
any myxml is the xml file that i want to parse right? so the datatype of myxml should be System.Xml.XmlDocument
is that right?
any myxml is the xml file that i want to parse right? so the datatype of myxml should be System.Xml.XmlDocument
is that right?
ASKER
the var resultALLInOne isthe collection, but how do i pass the collection back?
for example
public ???? testXml(System.Xml.XmlDocu ment lv_XMLDoc )
Regex postfixExpression = new Regex("(.*)(index|width)$" , RegexOptions.IgnoreCase);
// Get all in one collection
var resultAllInOne = from x in XDocument.Parse(lv_XMLDoc) .Root.Elem ents()
select new
{
Name = x.Name.LocalName,
Value = x.Value,
ColumnType = x.Name.LocalName.EndsWith( "index", StringComparison.OrdinalIg noreCase)? "Index" : "Width",
ColumnName = postfixExpression.Replace( x.Name.Loc alName, "$1")
};
}
need a bit of help
So the idea would be pass the xml document to this procedure,
i would use
public System.Xml.XmlDocument LoadXMLDoc(string XMLfile_directory
, string XMLfilename)
{
System.Xml.XmlDocument XMLDoc = new XmlDocument();
//Set up the filestream (READER) //
FileStream XMLFile = new FileStream
(XMLfile_directory + XMLfilename
, FileMode.Open
, FileAccess.Read
, FileShare.ReadWrite);
//Load the data from the file into the XMLDOC//
XMLDoc.Load(XMLFile);
return XMLDoc;
} //public void LoadXMLDoc
to load the file into and XML variable and then try to use the procedure you gave to put the values into a collection. and then pass the collection to the calling procedure
for example
public ???? testXml(System.Xml.XmlDocu
Regex postfixExpression = new Regex("(.*)(index|width)$"
// Get all in one collection
var resultAllInOne = from x in XDocument.Parse(lv_XMLDoc)
select new
{
Name = x.Name.LocalName,
Value = x.Value,
ColumnType = x.Name.LocalName.EndsWith(
ColumnName = postfixExpression.Replace(
};
}
need a bit of help
So the idea would be pass the xml document to this procedure,
i would use
public System.Xml.XmlDocument LoadXMLDoc(string XMLfile_directory
, string XMLfilename)
{
System.Xml.XmlDocument XMLDoc = new XmlDocument();
//Set up the filestream (READER) //
FileStream XMLFile = new FileStream
(XMLfile_directory + XMLfilename
, FileMode.Open
, FileAccess.Read
, FileShare.ReadWrite);
//Load the data from the file into the XMLDOC//
XMLDoc.Load(XMLFile);
return XMLDoc;
} //public void LoadXMLDoc
to load the file into and XML variable and then try to use the procedure you gave to put the values into a collection. and then pass the collection to the calling procedure
ASKER
ok made the corrections and here is a working solution
public string GetXmlString(XmlDocument xmlDoc)
{
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xmlDoc.WriteTo(xw);
return sw.ToString();
}
public Dictionary<string, string> XmlIndexWidth(XmlDocument lv_XMLDoc)
{
string XMLStr = GetXmlString(lv_XMLDoc);
Dictionary<string, string> ReturnDictionary = new Dictionary<string,string>( );
Regex postfixExpression = new Regex("(.*)(index|width)$" , RegexOptions.IgnoreCase);
// Get all in one collection
var resultAllInOne = from x in XDocument.Parse(XMLStr).Ro ot.Element s()
select new
{
Name = x.Name.LocalName,
Value = x.Value,
ColumnType = x.Name.LocalName.EndsWith( "index", StringComparison.OrdinalIg noreCase)? "Index" : "Width",
ColumnName = postfixExpression.Replace( x.Name.Loc alName, "$1")
};
foreach (var x in resultAllInOne)
{ReturnDictionary.Add(x.Na me, x.Value);}
return ReturnDictionary;
} //public Dictionary<string, string> XmlIndexWidth(XmlDocument lv_XMLDoc)
public Dictionary<string, string> XmlIndex(XmlDocument lv_XMLDoc)
{
string XMLStr = GetXmlString(lv_XMLDoc);
Dictionary<string, string> ReturnDictionary = new Dictionary<string, string>();
Regex postfixExpression = new Regex("(.*)(index|width)$" , RegexOptions.IgnoreCase);
// Retrieve only Index items
var resultIndexItems = from x in XDocument.Parse(XMLStr).Ro ot.Element s()
where x.Name.LocalName.EndsWith( "index", StringComparison.OrdinalIg noreCase)
select new
{
Name = x.Name.LocalName,
Value = x.Value,
ColumnType = "Index",
ColumnName = postfixExpression.Replace( x.Name.Loc alName, "$1")
};
foreach (var x in resultIndexItems)
{ ReturnDictionary.Add(x.Col umnName, x.Value); }
return ReturnDictionary;
}//public Dictionary<string, string> XmlIndex(XmlDocument lv_XMLDoc)
public Dictionary<string, string> XmlWidth(XmlDocument lv_XMLDoc)
{
string XMLStr = GetXmlString(lv_XMLDoc);
Dictionary<string, string> ReturnDictionary = new Dictionary<string, string>();
Regex postfixExpression = new Regex("(.*)(index|width)$" , RegexOptions.IgnoreCase);
// Retrieve only Width items
var resultWidthItems = from x in XDocument.Parse(XMLStr).Ro ot.Element s()
where x.Name.LocalName.EndsWith( "width", StringComparison.OrdinalIg noreCase)
select new
{
Name = x.Name.LocalName,
Value = x.Value,
ColumnType = "Width",
ColumnName = postfixExpression.Replace( x.Name.Loc alName, "$1")
};
foreach (var x in resultWidthItems)
{ ReturnDictionary.Add(x.Col umnName, x.Value); }
return ReturnDictionary;
}//public Dictionary<string, string> XmlWidth(XmlDocument lv_XMLDoc)
public string GetXmlString(XmlDocument xmlDoc)
{
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xmlDoc.WriteTo(xw);
return sw.ToString();
}
public Dictionary<string, string> XmlIndexWidth(XmlDocument lv_XMLDoc)
{
string XMLStr = GetXmlString(lv_XMLDoc);
Dictionary<string, string> ReturnDictionary = new Dictionary<string,string>(
Regex postfixExpression = new Regex("(.*)(index|width)$"
// Get all in one collection
var resultAllInOne = from x in XDocument.Parse(XMLStr).Ro
select new
{
Name = x.Name.LocalName,
Value = x.Value,
ColumnType = x.Name.LocalName.EndsWith(
ColumnName = postfixExpression.Replace(
};
foreach (var x in resultAllInOne)
{ReturnDictionary.Add(x.Na
return ReturnDictionary;
} //public Dictionary<string, string> XmlIndexWidth(XmlDocument lv_XMLDoc)
public Dictionary<string, string> XmlIndex(XmlDocument lv_XMLDoc)
{
string XMLStr = GetXmlString(lv_XMLDoc);
Dictionary<string, string> ReturnDictionary = new Dictionary<string, string>();
Regex postfixExpression = new Regex("(.*)(index|width)$"
// Retrieve only Index items
var resultIndexItems = from x in XDocument.Parse(XMLStr).Ro
where x.Name.LocalName.EndsWith(
select new
{
Name = x.Name.LocalName,
Value = x.Value,
ColumnType = "Index",
ColumnName = postfixExpression.Replace(
};
foreach (var x in resultIndexItems)
{ ReturnDictionary.Add(x.Col
return ReturnDictionary;
}//public Dictionary<string, string> XmlIndex(XmlDocument lv_XMLDoc)
public Dictionary<string, string> XmlWidth(XmlDocument lv_XMLDoc)
{
string XMLStr = GetXmlString(lv_XMLDoc);
Dictionary<string, string> ReturnDictionary = new Dictionary<string, string>();
Regex postfixExpression = new Regex("(.*)(index|width)$"
// Retrieve only Width items
var resultWidthItems = from x in XDocument.Parse(XMLStr).Ro
where x.Name.LocalName.EndsWith(
select new
{
Name = x.Name.LocalName,
Value = x.Value,
ColumnType = "Width",
ColumnName = postfixExpression.Replace(
};
foreach (var x in resultWidthItems)
{ ReturnDictionary.Add(x.Col
return ReturnDictionary;
}//public Dictionary<string, string> XmlWidth(XmlDocument lv_XMLDoc)
ASKER
I had to make a few chnages but the solution provided by Jo was far the beast and easy to follow for a novice like myself
ASKER