Jon said:
Sounds like izmjenaNode is null, presumably because it couldn't find
the right element.
Could you post a short but complete program which demonstrates the
problem?
See
http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
i have this two xml and i need to make third xml. i go trough obrazac
and if obrazac have attribute but no children than go to odjeljci and
find that odjeljak and write it to third. if obrazac have attribute and
children than i need to check if children is izmjena or red/redovi. if
it is izmjena than i need to take izmjena id and check for that id in
same odjeljak in odjeljci and write odjeljak from odjeljci with text
from izmjena at that id element. if name is red/redovi than i need to
take id and go to odjeljci and write just those red/redovi.
odjeljci:
<obrazac>
<odjeljak sifra="G.5">
<red num="1">
<text id="1">neki text</text>
</red>
<red num="2">
<stupac>
<checkbox id="5-1">text koji ide sa checkboxom</checkbox>
</stupac>
<stupac>
<text id="5-2">sfdgygasg</text>
<br />
<unos id="5-3" />
</stupac>
</red>
</odjeljak>
<odjeljak sifra="G.6" />
</obrazac>
other xml:, i have 20 xml like this. structure is same, but elements are
in different order or not all elements in
obrazac:
<obrazac>
<odjeljak>
<text>sudjelovanje</text>
<br />
<text>Datum: </text>
<unos id="29" />
<text>vrijeme: </text>
<unos id="30" />
</odjeljak>
<odjeljak sifra="G.5" />
<odjeljak>
<text>Ostali: </text>
<unos id=".31" />
</odjeljak>
<odjeljak>
<text>ponuda</text>
<br />
<text>Datum: </text>
<unos id="32" />
<br />
<checkbox id="C.14">Da</checkbox>
<checkbox id="C.15">Ne</checkbox>
<br />
<unos id="35" />
</odjeljak>
<odjeljak sifra="I.1">
<redovi od="1" do="7" />
</odjeljak>
</obrazac>
XmlDocument obrazac = LoadXmlDoc(@"..\..\XML\N-08-V.xml");
XPathNavigator obrazacNavigator = obrazac.CreateNavigator();
XPathExpression expresion = obrazacNavigator.Compile(@"/obrazac/odjeljak");
XPathNodeIterator obrazacIter = obrazacNavigator.Select(expresion);
XmlDocument odjeljci = LoadXmlDoc(@"..\..\XML\Odjeljci.xml");
XmlNode nodeObrazac;
XmlNode nodeOdjeljak;
//XmlTextWriter xmlWriter = new XmlTextWriter(@"RN-08-V.xml",
Encoding.UTF8);
XmlTextWriter xmlWriter = new XmlTextWriter(Console.Out);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingInstruction("xml", "version='1.0'
encoding='utf-8' ");
xmlWriter.WriteStartElement("obrazac");
xmlWriter.WriteRaw("\n");
string attrSifra;
string attrId;
while (obrazacIter.MoveNext())
{
...
else if (obrazacIter.Current.HasAttributes &&
obrazacIter.Current.HasChildren)
{
attrSifra = obrazacIter.Current.GetAttribute("sifra", "");
nodeObrazac = obrazac.SelectSingleNode(@"//odjeljak[@sifra='" +
attrSifra + "']");
nodeOdjeljak = odjeljci.SelectSingleNode(@"//odjeljak[@sifra='" +
attrSifra + "']");
if (nodeOdjeljak.Attributes["sifra"].Value == attrSifra)
{
xmlWriter.WriteStartElement("odjeljak");
foreach (XmlNode nod in nodeObrazac.ChildNodes)
{
if (nod.Name == "izmjena")
{
attrId = nod.Attributes["id"].Value;
foreach (XmlNode nod1 in nodeOdjeljak.ChildNodes)
{
if (nod1.Attributes["id"].Value == attrId)
{
xmlWriter.WriteStartElement(nod1.Name);
xmlWriter.WriteValue(nod1.Value);
xmlWriter.WriteFullEndElement();
}