471,354 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

xml/xpath parser problem

hi all!

can someone explain me why im getting this result?

this two <odjeljak<textshould be different

<?xml version='1.0' encoding='utf-8' ?>
<obrazac>
<odjeljak>
<odjeljak sifra="G.4"><red num="1"><text>III.1.4. Ostali posebni uvijeti za
izvršenje ugovora: (ako su određeni):</text></red><red num="2"><checkbox
id="1-1">Da</checkbox><checkbox id="1-2">Ne</checkbox></red><red
num="3"><text>Ako Da, opis posebnih uvjeta</text></red><red num="4"><unos
id="2-1" /></red></odjeljak>
</odjeljak>
<odjeljak>
<odjeljak sifra="G.4"><red num="1"><text>III.1.4. Ostali posebni uvijeti za
izvršenje ugovora: (ako su određeni):</text></red><red num="2"><checkbox
id="1-1">Da</checkbox><checkbox id="1-2">Ne</checkbox></red><red
num="3"><text>Ako Da, opis posebnih uvjeta</text></red><red num="4"><unos
id="2-1" /></red></odjeljak>
</odjeljak>
</obrazac>

N-16-M.xml
<odjeljci>
<odjeljak sifra="G.4">
<izmjena id="1">III.3.1. Izvršenje usluge je ograničeno na određenu
struku:</izmjena>
<izmjena id="2">Ako DA, upućivanje na odnosne pravne i upravne
propise:</izmjena>
</odjeljak>
<odjeljak sifra="G.4">
<izmjena id="1">III.1.4. Ostali posebni uvijeti za izvršenje ugovora: (ako
su određeni):</izmjena>
<izmjena id="2">Ako Da, opis posebnih uvjeta</izmjena>
</odjeljak>
</odjeljci>

odjeljci.xml
<odjeljci>
<odjeljak sifra="G.4">
<red num="1">
<text id="1" />
</red>
<red num="2">
<checkbox id="1-1">Da</checkbox>
<checkbox id="1-2">Ne</checkbox>
</red>
<red num="3">
<text id="2" />
</red>
<red num="4">
<unos id="2-1" />
</red>
</odjeljak>
</odjeljci>
public class GeneriranjeObrazaca
{
XmlDocument obrazac;
XmlDocument odjeljci;

XPathNavigator obrazacNavigator;
XPathNavigator odjeljciNavigator;
XPathNodeIterator obrazacIter;
XPathNodeIterator odjeljciIter;
XPathNodeIterator childrenIter;

XmlTextWriter xmlWriter;

string attrSifra;
string attrId;

public GeneriranjeObrazaca(string path)
{
obrazac = LoadXmlDoc(path);
obrazacNavigator = obrazac.CreateNavigator();
obrazacIter = obrazacNavigator.Select(@"/obrazac/odjeljak");

odjeljci = LoadXmlDoc(@"..\..\XML\Odjeljci.xml");
odjeljciNavigator = odjeljci.CreateNavigator();

xmlWriter = new XmlTextWriter(Console.Out);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingInstruction("xml", "version='1.0'
encoding='utf-8' ");
}

public XmlDocument LoadXmlDoc(string path)
{
XmlDocument doc;
try
{
doc = new XmlDocument();
doc.Load(path);
return doc;
}
catch (XmlException e)
{
Console.WriteLine(e.Message);
}
return null;
}

public void StvoriObrazac()
{
XmlDocument tempDoc = new XmlDocument();

xmlWriter.WriteStartElement("obrazac");
xmlWriter.WriteRaw("\n");

while (obrazacIter.MoveNext())
{
if (!obrazacIter.Current.HasAttributes)
{
//xmlWriter.WriteRaw(obrazacIter.Current.OuterXml);
//xmlWriter.WriteRaw("\n");
}
else if (obrazacIter.Current.HasAttributes &&
(!obrazacIter.Current.HasChildren))
{
attrSifra = obrazacIter.Current.GetAttribute("sifra", "");
odjeljciIter =
odjeljciNavigator.Select(@"//odjeljak[@sifra='" + attrSifra + "']");

while (odjeljciIter.MoveNext())
{
if (odjeljciIter.Current.GetAttribute("sifra", "") ==
attrSifra)
{
//xmlWriter.WriteRaw(odjeljciIter.Current.OuterXml);
//xmlWriter.WriteRaw("\n");
}
}
}
else if (obrazacIter.Current.HasAttributes &&
obrazacIter.Current.HasChildren)
{
attrSifra = obrazacIter.Current.GetAttribute("sifra", "");
int[] attrList = new int[2];

childrenIter =
obrazacNavigator.Select(@"//odjeljak[@sifra='" + attrSifra + "']/child::*");
odjeljciIter =
odjeljciNavigator.Select(@"//odjeljak[@sifra='" + attrSifra + "']");

odjeljciIter.MoveNext();

tempDoc.LoadXml(odjeljciIter.Current.OuterXml);

xmlWriter.WriteStartElement("odjeljak");
xmlWriter.WriteString("\n");

while (childrenIter.MoveNext())
{
if (childrenIter.Current.HasChildren)
{
if (childrenIter.Current.Name == "izmjena")
{
attrId =
childrenIter.Current.GetAttribute("id", "");

XmlNodeList nodeList =
tempDoc.GetElementsByTagName("*");

for (int i = 0; i < nodeList.Count; i++)
{
try
{
if (nodeList[i].Attributes["id"].Value
== attrId)
{
XmlNode root = nodeList[i].ParentNode;
XmlElement el =
tempDoc.CreateElement(nodeList[i].Name);
el.InnerText =
childrenIter.Current.Value;
root.ReplaceChild(el, nodeList[i]);
}
}
catch (NullReferenceException)
{ }
}
}
Jun 27 '08 #1
0 1104

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bdinmstig | last post: by
2 posts views Thread by Piet | last post: by
3 posts views Thread by Johannes Koch | last post: by
1 post views Thread by andy | last post: by
3 posts views Thread by abcd_68 | last post: by
4 posts views Thread by Pyrus | last post: by
1 post views Thread by Floris Bruynooghe | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.