XmlNode

G

gigs

hi

im getting this error when i tried to write start element and when i try to
write value: Object reference not set to an instance of an object.

XmlNode node;
XmlNode izmjenaNode;

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

foreach (XmlNode nod in node.ChildNodes)
{
if (nod.Name == "izmjena")
{
attrId = nod.Attributes["id"].Value;
izmjenaNode = odjeljci.GetElementById(attrId);
xmlWriter.WriteStartElement(izmjenaNode.Name);
xmlWriter.WriteValue(izmjenaNode.Value);
xmlWriter.WriteFullEndElement();
}
}


what is the problem here_

thanks
 
J

Jon Skeet [C# MVP]

gigs said:
im getting this error when i tried to write start element and when i try to
write value: Object reference not set to an instance of an object.

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.
 
G

gigs

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();
}
 
G

gigs

gigs said:
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();
}


can you give me any suggestion, some better approach to this problem? thanks!
 
J

Jon Skeet [C# MVP]

gigs said:
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.

Please post a sample program which is *complete*. I'm happy to copy and
paste an XML file, but the code you post should just compile, on its
own, with no extras (such as using directives) needed.
 
G

gigs

Jon said:
Please post a sample program which is *complete*. I'm happy to copy and
paste an XML file, but the code you post should just compile, on its
own, with no extras (such as using directives) needed.
here it is.
i have problem with code after 53 line - else if
(obrazacIter.Current.HasAttributes && obrazacIter.Current.HasChildren)




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using System.Security;

namespace Obrasci
{
public class GeneriranjeObrazaca
{
public void UcitavanjeXmla()
{
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())
{
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", "");
nodeOdjeljak =
odjeljci.SelectSingleNode(@"//odjeljak[@sifra='" + attrSifra + "']");

if (nodeOdjeljak.Attributes["sifra"].Value == attrSifra)
{
//xmlWriter.WriteRaw(nodeOdjeljak.OuterXml);
//xmlWriter.WriteRaw("\n");
}
}
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();
}
else
xmlWriter.WriteRaw("S");
}
}
}
}
}
}
xmlWriter.WriteFullEndElement();
xmlWriter.Close();
}
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;
}
}
 
J

Jon Skeet [C# MVP]

gigs said:
here it is.
i have problem with code after 53 line - else if
(obrazacIter.Current.HasAttributes && obrazacIter.Current.HasChildren)

<snip>

Okay. In the sample files you provided, there was:

<odjeljak sifra="G.6" />

in the first file, but not in the second. Assuming I've got the first
the right way round, it's trying to find a similar element in the
second file, but that doesn't exist - so SelectSingleNode is returning
null. You're then trying to use that null value.

You should look at the value returned by SelectSingleNode and take
appropriate action if it's null.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top