0

I need to find the nodes that have the /CFOP/ tag with 5902 as value, so it always end up being all the itens with the tag /det nItem="x"/ with x being always > 1. So what can i do to make it work?

The XML file: https://drive.google.com/file/d/1-ayEd1PSP9rECeyYesx3dTndInlqNouR/view?usp=sharing

My code is opening the file and trying to erase all the nodes "det nItem > 2", here's what i've done from my researches. But it only opens and save the file without any changes.

using System.Xml;

XmlDocument xml = new XmlDocument();
xml.Load(filename: "C:/Users/A376228/Desktop/xml/ped1.xml");
XmlNodeList nodeList = xml.SelectNodes(xpath: "/infNFe/det[@CFOP=" + 5902 + "]");

foreach (XmlNode node in nodeList)
{
    node.ParentNode.RemoveChild(node);
}
xml.Save(filename: "C:/Users/A376228/Desktop/xml/ped1-ready.xml");

Thanks for your time!

1 Answer 1

1

Use the XmlNamespaceManager. This code should delete the single node where nItem > 2:

XmlDocument xml = new XmlDocument();
xml.Load(filename: "C:/Users/A376228/Desktop/xml/ped1.xml");
XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xml.NameTable);
xmlNamespaceManager.AddNamespace("x", "http://www.portalfiscal.inf.br/nfe");
XmlNodeList nodeList = xml.DocumentElement.SelectNodes(xpath: "//x:infNFe/x:det[@nItem > 2]",
    xmlNamespaceManager);

foreach (XmlNode node in nodeList)
{
    node.ParentNode.RemoveChild(node);
}
xml.Save(filename: "C:/Users/A376228/Desktop/xml/ped1-ready.xml");
Sign up to request clarification or add additional context in comments.

3 Comments

I've managed to do it in batch on a folder
can you explain me this line? < xmlNamespaceManager.AddNamespace("x", "portalfiscal.inf.br/nfe"); >
It maps the namespace to a prefix to be used when searching for the nodes: learn.microsoft.com/en-us/dotnet/api/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.