all groups > dotnet xml > june 2007 >
You're in the

dotnet xml

group:

need help with XML parser


need help with XML parser Bill Nguyen
6/25/2007 3:23:07 PM
dotnet xml:
I can parse a simple XML file (the hard way) but do not know how to parse
the XML file (DTD and sample XML below) using System.XML.
YOur help is greatly appreciated.

Bill


<!-- DTN REFINED FUELS XML FUEL PRICE FORMAT 2-->
<!ELEMENT PRICES (TransmissionHeader, FuelPriceData+)>

<!ELEMENT AdjustedPrice (#PCDATA)>
<!ELEMENT BrandedCode (#PCDATA)>
<!ELEMENT City (#PCDATA)>
<!ELEMENT Change (#PCDATA)>
<!ELEMENT EffectiveDate (#PCDATA)>
<!ELEMENT EffectiveTime (#PCDATA)>
<!ELEMENT FuelPriceData (SupplierName, Terminal, Product)>
<!ELEMENT IsContract (#PCDATA)>
<!ELEMENT IsOutage (#PCDATA)>
<!ELEMENT Name (#PCDATA)>
<!ATTLIST Name
identType (TerminalID) #IMPLIED
ident CDATA #IMPLIED
[quoted text, click to view]
<!ELEMENT PostedPrice (#PCDATA)>
<!ELEMENT Product (ProductName, BrandedCode, EffectiveDate, EffectiveTime,
PostedPrice, AdjustedPrice, Change, IsContract, IsOutage, TVA)>
<!ELEMENT ProductName (#PCDATA)>
<!ATTLIST ProductName
identType (ProductID) #IMPLIED
ident CDATA #IMPLIED
[quoted text, click to view]
<!ELEMENT State (#PCDATA)>
<!ELEMENT SupplierName (#PCDATA)>
<!ATTLIST SupplierName
identType (SupplierID) #IMPLIED
ident CDATA #IMPLIED
[quoted text, click to view]
<!ELEMENT Terminal (Name, City, State, TerminalOwner)>
<!ELEMENT TerminalOwner (#PCDATA)>
<!ATTLIST TerminalOwner
identType (TermOwnerID) #IMPLIED
ident CDATA #IMPLIED
[quoted text, click to view]
<!ELEMENT TransmissionAgency (#PCDATA)>
<!ELEMENT TransmissionDate (#PCDATA)>
<!ELEMENT TransmissionHeader (TransmissionDate, TransmissionTime,
TransmissionAgency)>
<!ELEMENT TransmissionTime (#PCDATA)>
<!ELEMENT TVA (#PCDATA)>




<?xml version="1.0"?>

<!DOCTYPE PRICES SYSTEM "dtnxml_PriceDoc2.dtd">

<PRICES>

<TransmissionHeader>

<TransmissionDate>20070620</TransmissionDate>

<TransmissionTime>1447</TransmissionTime>

<TransmissionAgency>DTN Integrated
Services/dtnDataConnect</TransmissionAgency>

</TransmissionHeader>

<FuelPriceData>

<SupplierName identType="SupplierID" ident="0032">Hardy Oil
PB2</SupplierName>

<Terminal>

<Name identType="TerminalID" ident="1288">DMns Magellan</Name>

<City>Des Moines</City>

<State>IA</State>

<TerminalOwner identType="TermOwnerID" ident="0275">Magellan Pipeline
Company</TerminalOwner>

</Terminal>

<Product>

<ProductName identType="ProductID" ident="0024">08 Conv 87 Unleaded
Octane</ProductName>

<BrandedCode>U</BrandedCode>

<EffectiveDate>20051005</EffectiveDate>

<EffectiveTime>0001</EffectiveTime>

<PostedPrice>2.132500</PostedPrice>

<AdjustedPrice>2.132500</AdjustedPrice>

<Change>.000000</Change>

<IsContract>N</IsContract>

<IsOutage>N</IsOutage>

<TVA/>

</Product>

</FuelPriceData>

<FuelPriceData>

<SupplierName identType="SupplierID" ident="0032">Hardy Oil
PB2</SupplierName>

<Terminal>

<Name identType="TerminalID" ident="1410">Lsvl BP Oil</Name>

<City>Louisville</City>

<State>KY</State>

<TerminalOwner identType="TermOwnerID" ident="0017">BP Oil
Products</TerminalOwner>

</Terminal>

<Product>

<ProductName identType="ProductID" ident="0017">16 Low Sulfur #2 Diesel
Dyed</ProductName>

<BrandedCode>U</BrandedCode>

<EffectiveDate>20051004</EffectiveDate>

<EffectiveTime>1800</EffectiveTime>

<PostedPrice>2.584000</PostedPrice>

<AdjustedPrice>2.584000</AdjustedPrice>

<Change>.000000</Change>

<IsContract>N</IsContract>

<IsOutage>N</IsOutage>

<TVA/>

</Product>

</FuelPriceData>

</PRICES>

Re: need help with XML parser Martin Honnen
6/26/2007 2:11:50 PM
[quoted text, click to view]

With .NET 2.0 use
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ProhibitDtd = false
readerSettings.ValidationType = ValidationType.DTD;
using (XmlReader xmlReader = XmlReader.Create(@"file.xml",
readerSettings)
{
while (xmlReader.Read())
{
// extract data here if necessary
}
}


--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button