SelectNodes brings back to many results - c#

Loading this XML works
<?xml version="1.0" encoding="utf-8" ?>
<export>
<document ID="uuuid_1">
<Property Name="PersonName" Value="bob"></Property>
<Property Name="FileName" Value="bob.tif">
<Reference Link="company\export\uuuid_1_423_bob.tif"/>
</Property>
<Property Name="FileName" Value="bob.txt">
<Reference Link="company\export\uuuid_1_123_bob.txt"/>
</Property>
<Property Name="FileName" Value="bob.tif">
<Reference Link="company\export\uuuid_1_123_bob.tif"/>
</Property>
</document>
<document ID="uuuid_2">
<Property Name="PersonName" Value="mary"></Property>
<Property Name="FileName" Value="mary.tif">
<Reference Link="company\export\uuuid_2_456_mary.tif"/>
</Property>
<Property Name="FileName" Value="mary.txt">
<Reference Link="company\export\uuuid_2_567_mary.txt"/>
</Property>
</document>
</export>
with that method
static void XmlLoader(string xml_path)
{
Console.WriteLine("Loading " + xml_path);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xml_path);
XmlNodeList nodes_document = xmldoc.SelectNodes("/export/document");
foreach (XmlNode nd in nodes_document)
{
string Id = nd.Attributes["ID"].Value.ToString();
string name = nd.SelectSingleNode("//Property[#Name='PersonName']/#Value").InnerText;
XmlNodeList files = nd.SelectNodes("//Property[#Name='FileName'][contains(#Value,'.tif')]/Reference/#Link");
Console.WriteLine(files.ToString());
}
}
The XmlNodeList inside the iteration of documents brings back a list of ALL tifs in the XML not only the ones from the nd Node.
How would I correctly use Xpath to select a list inside the nd element?

Just remove "//" from SelectNodes and SelectSingleNode. The double slash is parsing the complete xml
static void XmlLoader(string xml_path)
{
Console.WriteLine("Loading " + xml_path);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xml_path);
XmlNodeList nodes_document = xmldoc.SelectNodes("/export/document");
foreach (XmlNode nd in nodes_document)
{
string Id = nd.Attributes["ID"].Value.ToString();
string name = nd.SelectSingleNode("Property[#Name='PersonName']/#Value").InnerText;
XmlNodeList files = nd.SelectNodes("Property[#Name='FileName'][contains(#Value,'.tif')]/Reference/#Link");
Console.WriteLine(files.ToString());
}
}

Related

NullReferenceException unhandled by user code while changing XML document

I am getting the error : NullReferenceException was unhandled by user code , object reference not set to an instance of an object. While I want to change the element of an xml file. this is the code the error is generated on :
doc2.SelectSingleNode("/Document/CstmrCdtTrfInitn/GrpHdr/MsgId", nsmgr).InnerText = bank.Afkorting + "-0001";
This is the previous code :
XmlDocument doc2 = new XmlDocument();
doc2.Load(#"C:\Users\Kevin\Desktop\xsd\betalingen_sepa.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc2.NameTable);
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
DagboekFinancieel bank = os.FindObject<DagboekFinancieel>(CriteriaOperator.Parse("[Omschrijving] = ?", opdracht.Bank));
doc2.SelectSingleNode("/Document/CstmrCdtTrfInitn/GrpHdr/MsgId", nsmgr).InnerText = bank.Afkorting + "-0001";
string naamFile = bank.Afkorting + "-0001";
doc2.Save(#"C:\" + naamFile + ".xml");
This is the original XML file I got :
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
<CstmrCdtTrfInitn>
<GrpHdr>
<MsgId>#ref_xml#</MsgId>
<CreDtTm>#datum_tijd_xml#</CreDtTm>
<NbOfTxs>#aantal_transacties#</NbOfTxs>
<InitgPty>
<Nm>#afzender_naam#</Nm>
<Id>
<OrgId>
<Othr>
<Id>#afzender_btw_nr#</Id>
<Issr>KBO-BCE</Issr>
</Othr>
</OrgId>
</Id>
</InitgPty>
</GrpHdr>
<PmtInf>
<PmtInfId>#ref_payment_block#</PmtInfId>
<PmtMtd>TRF</PmtMtd>
<BtchBookg>true</BtchBookg>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
</PmtTpInf>
<ReqdExctnDt>#datum_uitvoering#</ReqdExctnDt>
<Dbtr>
<Nm>#afzender_naam#</Nm>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>#afzender_iban#</IBAN>
</Id>
</DbtrAcct>
<DbtrAgt>
<FinInstnId>
<BIC>#afzender_bic#</BIC>
</FinInstnId>
</DbtrAgt>
<ChrgBr>#kosten#</ChrgBr>
<CdtTrfTxInf>
<PmtId>
<EndToEndId>#ref_end_to_end#</EndToEndId>
</PmtId>
<Amt>
<InstdAmt Ccy="#munt#">#bedrag#</InstdAmt>
</Amt>
<CdtrAgt>
<FinInstnId>
<BIC>#leveran_bic#</BIC>
</FinInstnId>
</CdtrAgt>
<Cdtr>
<Nm>#leveran_naam#</Nm>
<PstlAdr>
<Ctry>#leveran_land#</Ctry>
<AdrLine>#leveran_straat#</AdrLine>
<AdrLine>#leveran_wpl#</AdrLine>
</PstlAdr>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>#leveran_iban#</IBAN>
</Id>
</CdtrAcct>
<RmtInf>
<Ustrd>#leveran_ref#</Ustrd>
<Strd>
<CdtrRefInf>
<Tp>
<CdOrPrtry>
<Cd>#gestruct_mededeling_soort1#</Cd>
</CdOrPrtry>
<Issr>#gestruct_mededeling_soort2#</Issr>
</Tp>
<Ref>#gestruct_mededeling#</Ref>
</CdtrRefInf>
</Strd>
</RmtInf>
</CdtTrfTxInf>
</PmtInf>
</CstmrCdtTrfInitn>
</Document>
Your xml namespace is not correct. Use it like
nsmgr.AddNamespace("ns", "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03");
doc2.SelectSingleNode("/ns:Document/ns:CstmrCdtTrfInitn/ns:GrpHdr/ns:MsgId", nsmgr).InnerText = "some text";

insert element after selected node

Am trying to add a new element called entity after the last entity but it keeps adding it inside the selected entity. To understand better this is my xml sample.
<Root>
<Class Name="ECMInstruction" Style="Top">
<Entity Id="1" Name="DocumentInformation" />
<Entity Id="2" Name="CustomerInformation" />
<Property Id="1" Name="DocumentTitle">
</Property>
<Property Id="2" Name="DateCreated">
<Lists>
<ListName>ws_Users</ListName>
<ListName>dfdfdfd</ListName>
</Lists>
</Property>
<Property Id="3" Name="Deadline">
</Property>
</Class>
</Root>
This is how it looks like after is inserted. I've tried using insertAfter but it gives me error.
<Entity Id="1" Name="DocumentInformation">
<Entity Id="2" Name="sds" />
</Entity>
The code:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("sample.xml");
XmlNodeList cnode = xmldoc.DocumentElement.SelectNodes("//Class[#Name='" + CurrentClass + "']/Entity");
foreach (XmlNode c in cnode)
{
int value = 0;
String entitycount = cnode.Count.ToString();
int.TryParse(entitycount, out value);
value = value + 1;
XmlElement root = xmldoc.CreateElement("Entity");
root.SetAttribute("Id", value.ToString());
root.SetAttribute("Name", EntityNametxt.Text);
c.AppendChild(root);
xmldoc.Save("sample.xml");
}
"I've tried using insertAfter but it gives me error. "
As per documentation, InsertAfter() should be called on parent node of referenced XmlNode (the 2nd argument of the method), otherwise ArgumentException will be thrown :
//instead of this : c.AppendChild(root);
//..you could do as follow :
c.ParentNode.InsertAfter(root, c);

delete child node with specific value

Am trying to delete a list name containing the value "dfdfdfd" from an XPath
XmlNode names = LoadDocument(xml).DocumentElement.SelectSingleNode("//Class[#Name='" + getCurClass() + "']/Property[#Id='" + i + "']/Lists[contains(ListName,'ws_Users')]");
after I execute this statement:
names.RemoveChild(names.FirstChild);
but I nothing happens.
My XML :
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Class Name="ECMInstruction" Style="Top">
<Entity Id="1" Name="DocumentInformation" />
<Property Id="1">
</Property>
<Property Id="2">
<Lists>
<ListName>ws_Users</ListName>
<ListName>dfdfdfd</ListName>
</Lists>
</Property>
</Class>
</Root>
Thanks for the help
You need to save the modified XmlDocument object back to file :
XmlDocument doc = LoadDocument(xml);
XmlNode names = doc.DocumentElement.SelectSingleNode("//Class[#Name='" + getCurClass() + "']/Property[#Id='" + i + "']/Lists[contains(ListName,'ws_Users')]");
names.RemoveChild(names.FirstChild);
//save `doc` back to file :
doc.Save("path_to_the_xml_file.xml");
or this way :
XmlNode names = LoadDocument(xml).DocumentElement.SelectSingleNode("//Class[#Name='" + getCurClass() + "']/Property[#Id='" + i + "']/Lists[contains(ListName,'ws_Users')]");
names.RemoveChild(names.FirstChild);
//save owner `XmlDocument` back to file :
names.OwnerDocument.Save("path_to_the_xml_file.xml");

XDocument losing line number

I'm using XDocument to parse my XML File, but when I try to read the line number of a XNode or a XElement, it's always equal to zero.
I tried different ways to parse it:
foreach (XElement node in xDoc.Root.Descendants("nodeName"))
or
xDoc.XPathSelectElement("nodeName")
and each time ((IXmlLineInfo)node).LineNumber returns 0!
I'm using a XmlNamespaceManager.
Did I miss something?
Thanks in advance!
Edit :
Here is the concerned Xml as asked.
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<CstmrDrctDbtInitn>
<GrpHdr>
<MsgId>XXXXXXXXX</MsgId>
<CreDtTm>2013-06-12T00:00:00</CreDtTm>
<NbOfTxs>1</NbOfTxs>
<CtrlSum>136.82</CtrlSum>
<InitgPty>
<Nm>name</Nm>
<Id>
<OrgId>
<Othr>
<Id>XXXXXXXXX</Id>
</Othr>
</OrgId>
</Id>
</InitgPty>
</GrpHdr>
<PmtInf>
<PmtInfId>275-20130612-FIRST</PmtInfId>
<PmtMtd>DD</PmtMtd>
<BtchBookg>true</BtchBookg>
<NbOfTxs>1</NbOfTxs>
<CtrlSum>136.82</CtrlSum>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
<LclInstrm>
<Cd>CORE</Cd>
</LclInstrm>
<SeqTp>RCUR</SeqTp>
</PmtTpInf>
<ReqdColltnDt>2013-06-05</ReqdColltnDt>
<Cdtr>
<Nm>name</Nm>
<PstlAdr>
<Ctry>BE</Ctry>
<AdrLine>XXXXXXXXX</AdrLine>
<AdrLine>XXXXXXXXX</AdrLine>
</PstlAdr>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>XXXXXXXXX</IBAN>
</Id>
</CdtrAcct>
<CdtrAgt>
<FinInstnId>
<BIC>XXXXXXXXX</BIC>
</FinInstnId>
</CdtrAgt>
<ChrgBr>SLEV</ChrgBr>
<CdtrSchmeId>
<Id>
<PrvtId>
<Othr>
<Id>XXXXXXXXX</Id>
<SchmeNm>
<Prtry>SEPA</Prtry>
</SchmeNm>
</Othr>
</PrvtId>
</Id>
</CdtrSchmeId>
<DrctDbtTxInf>
<PmtId>
<InstrId>XXXXXXXXX</InstrId>
<EndToEndId>XXXXXXXXX</EndToEndId>
</PmtId>
<InstdAmt Ccy="EUR">136.82</InstdAmt>
<DrctDbtTx>
<MndtRltdInf>
<MndtId>XXXXXXXXX</MndtId>
<DtOfSgntr>2009-10-31</DtOfSgntr>
<AmdmntInd>false</AmdmntInd>
<AmdmntInfDtls>
<AmdmntInd>yellowland</AmdmntInd>
<OrgnlMndtId>XXXXXXXXX</OrgnlMndtId>
<OrgnlCdtrSchmeId>
<Id>
<PrvtId>
<Othr>
<Id>XXXXXXXXX</Id>
<SchmeNm>
<Prtry>SEPA</Prtry>
</SchmeNm>
</Othr>
</PrvtId>
</Id>
</OrgnlCdtrSchmeId>
</AmdmntInfDtls>
</MndtRltdInf>
</DrctDbtTx>
<DbtrAgt>
<FinInstnId>
<BIC>XXXXXXXXX</BIC>
</FinInstnId>
</DbtrAgt>
<Dbtr>
<Nm>TEST</Nm>
<PstlAdr>
<Ctry>BE</Ctry>
<AdrLine>XXXXXXXXX</AdrLine>
<AdrLine>XXXXXXXXX</AdrLine>
</PstlAdr>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>XXXXXXXXX</IBAN>
</Id>
</DbtrAcct>
<RmtInf>
<Ustrd>test</Ustrd>
</RmtInf>
</DrctDbtTxInf>
</PmtInf>
</CstmrDrctDbtInitn>
</Document>
The LineInformations are not always loaded, when you load xml via XDocument.
You need to specify that you also want to load the LineInformation when you load the XML. That is done by using one of the Load methods that you can pass in a value of LoadOptions of the XDocument class.
var document = XDocument.Load(file, LoadOptions.SetLineInfo);
From here
XDocument xdoc = XDocument.Load(file);
IEnumerable<XElement> nodes = xdoc.Descendants("nodeName");
foreach (XElement node in nodes)
{
IXmlLineInfo info = node;
int lineNumber = info.LineNumber;
}

Reading an XML file into a TreeView

I am trying to load an XML file onto my GUI using a TreeView control.
However, I am using a proprietary layout for my XML file.
The XML is structured like this:
<ConfiguratorConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Section>
<Class ID="Example" Name="CompanyName.Example" Assembly="Example.dll">
<Interface>
<Property Name="exampleProperty1" Value="exampleValue" />
<Property Name="exampleProperty2" Value="exampleValue" />
<Property Name="exampleProperty3" Value="exampleValue" />
</Interface>
</Class>
</Section>
</ConfiguratorConfig>
I'd like the output to be structured like:
Class "Example"
Property "exampleProperty1"
Property "exampleProperty2"
Property "exampleProperty3"
I'm totally new to using XML. I've been searching the web for the past few hours, and none of the results have helped. Some have come close, but perhaps properties won't show up, or node's names won't display, etc.
I'm writing in c# in Visual Studio 2005.
Thanks for the help!
You can iterate through nodes using XmlDocument, you can put this demo in a Main method of a console application:
string xml = #"<ConfiguratorConfig xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<Section>
<Class ID=""Example"" Name=""CompanyName.Example"" Assembly=""Example.dll"">
<Interface>
<Property Name=""exampleProperty1"" Value=""exampleValue"" />
<Property Name=""exampleProperty2"" Value=""exampleValue"" />
<Property Name=""exampleProperty3"" Value=""exampleValue"" />
</Interface>
</Class>
</Section></ConfiguratorConfig>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlNode _class in doc.SelectNodes(#"/ConfiguratorConfig/Section/Class"))
{
string name = _class.Attributes["ID"].Value;
Console.WriteLine(name);
foreach (XmlElement element in _class.SelectNodes(#"Interface/Property"))
{
if (element.HasAttribute("Name"))
{
string nameAttributeValue = element.Attributes["Name"].Value;
Console.WriteLine(nameAttributeValue);
}
}
}
If you're using version of .NET higher than 3.0 you can use XDocument class (recommended if you can choose).
XDocument xdoc = XDocument.Parse(xml);
foreach (XElement _class in xdoc.Descendants("Class"))
{
string name = _class.Attribute("ID").Value;
Console.WriteLine(name);
foreach (XElement element in _class.Descendants("Property"))
{
XAttribute attributeValue = element.Attribute("Name");
if (attributeValue != null)
{
string nameAttributeValue = attributeValue.Value;
Console.WriteLine(nameAttributeValue);
}
}
}

Categories