Delete node from xml documents - c#

I'm new in the programming world.
I'm just looking for help with some kind of code, that would delete node from bunch of xml documents.
It is possible to make something which would delete node in bunch of xml documents at once?

There are many different technologies you could use, which is a bit daunting if you are new to programming. Since this quite a simple task, it's probably not worth investing a lot of time learning new tools: but then it all depends on what you're already comfortable with.
Many people would use XSLT for any job that involves modifying XML documents. You could write an XSLT 3.0 stylesheet transform.xsl like this:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="deleted-node"/>
</xsl:transform>
where "deleted-node" is the name of the nodes you want to delete (or a more complex pattern if you need it). And then you could apply this to all XML files in a directory in, putting the result in directory out, using the Saxon XSLT processor from the command line like this:
Transform -s:in -o:out -xsl:transform.xsl
The way this works is that xsl:mode defines the default processing to be applied to nodes if there isn't a more specific rule; shallow-copy means that you copy the tags and then move on to process the content. There's only one more specific rule, which matches the elements you want to delete; the rule is empty indicating that when you hit one of these elements, you output nothing.

Related

structure of selfnodes changes when creating an xml file from another

while creating an xml file from another one by cloning nodes from source to target file in c#, the structure of empty nodes like <noeud></noeud> becomes <noeud/>
i've tried this :
if (nodeSource.InnerText.Equals(""))
XmlNode nodeDestination = NodeSource.CloneNode(false);
is there any method to keep the same structure .
The format <element/> is frequently called a self-closing element. It's 100% valid, and the preferred storage method. If you really care (why?) re-writing to expanded format (<element></element>), you can look at writing your own XmlTextWriter. This article will be helpful for you.
http://blogs.msdn.com/b/nareshjoshi/archive/2009/01/15/how-to-force-non-self-closing-tags-for-empty-nodes-when-using-xslcompiledtransform-class.aspx

Parse XML with regards to one namespace only

I need to parse XML files with regards to only one namespace.
By "with regards to only one namespace" I mean that if I have document like this:
<xc:document xmlns:xc="asdasd">
<asdf>
<xc:abcd />
</asdf>
</xc:document>
I would like <asdf>, </asdf> to be treated as text.
The structure of this document should look like this:
document
|
|- text (<asdf>)
|- abcd
|- text (</asdf>)
What is the simplest method to achieve this?
Transform the document with xslt first so that the nodes you want treated as text actually are text.
Pretty much any XML parser is going to lose distinctions like whether single or double quotes were used, or CDATA sections were used, or whitespace inside tags (not between tags).
So:
<boy socks="black"
></boy>
might come back as <boy socks='black'/>
If you want to treat the input as not XML, you'll have to fall back on non-XML tools, or rethink your situation entirely, as this is a very unusual thing to want to do.
It's fairly easy in a text-processing language such as Perl, if you are careful. For example,
perl -p -e 's#<(/?[^:]+[\s>])#\<$1#g'
will go a long way, by changing the < signs you want to treat as text into < instead. This approach actually works best if you read the whole file in Perl rather than (as in this example) a line at a time, so that you can match close tags spread over multiple lines,
</boy
> like this.
But, best to parse XML with an XML parser, not regular expressions, so if the sort of changes I mentioned above are OK, this is really easy to do in XSLT.

Merging two xml files in C# without appending and without deleting anything (example given)

So say I have one xml file such as this:
<shapes>
<shape>shape1</shape>
</shapes>
And another xml file like this:
<parentNode>
<shapes>
<shape>shape 2</shape>
</shapes>
</parentnode>
I would like the output to be:
<parentNode>
<shapes>
<shape>shape1</shape>
<shape>shape 2</shape>
</shapes>
</parentnode>
The context is that I am using the visio schema but I wish the config file for an application which writes visio xml files to be a stripped down version of a visio config file. It should allow users to change shape properties, e.g. "process" to have a yellow colour AND it should allow them to add new shapes for example "AccountsTable" which the application will search for before using a standard shape and use the custom shape instead in some circumstances.
In terms of the merge it basically needs to stick the right leaf nodes in the right places if that makes sense? Without overwriting anything unless the config file has been explicitly written to do so, e.g. a custom "shape 2".
What should I be looking at to achieve this? The dataset method is pretty useless.
Many thanks!!!
You can load both files into two XElement objects, locate the target nodes in both objects and add or remove as you wish.
Here is a sample:
var doc1 = XDocument.Parse(file1).Element("shapes");
var doc2 = XDocument.Parse(file2).Element("parentNode").Element("shapes");
doc2.Add(doc1.Nodes());
I don't think there is an easy solution. Considering that you are not restricted to merging the contents of the Shapes node, i think you will have to parse through the nodes of one of the document recursively, checking whether each of these nodes is present in the other document through XPath. And once you find a node that is common in both the documents, you can merge the contents of one in the other. It is hardly efficient and there may be a better way but thats the best I can think of.
Psuedo code, I am guessing at the method names.
...
xmlreader xmlToMerge1 = xmlreader.create(XmlSourceVariableHere);
xmlreader xmlToMerge2 = xmlreader.create(XmlSourceVariableToMergeHere);
xmlwriter xmlout = new xmlwriter(someStreamOrOther);
xmlout.writeBeginElement("parentnode");
xmlout.writeBeginElement("shapes");
while (xmlToMerge1.Read())
{
if (xmlreader.nodetype == element && xml.Name == "shape")
{
xmlToMerge1.WriteNodeTo(xmlout);
}
}
while (xmlToMerge2.Read())
{
if (xmlToMerge2.nodetype == element && xmlToMerge2.Name == "shape")
{
xmlToMerge2.WriteNodeTo(xmlout);
}
}
xmlout.writeEndNode(); // end shapes
xmlout.writeEndNode(); // end parentnode
I remember that there is a command to write a node from a reader to a writer, but I don't remember what it is specifically, you'll have to look that one up.
What exactly do you mean by the following?
In terms of the merge it basically
needs to stick the right leaf nodes in
the right places if that makes sense?
Without overwriting anything unless
the config file has been explicitly
written to do so, e.g. a custom "shape
2".
You'll have to explain your requirements a bit more if you want an answer to be more detailed than simply merging nodes.

Parsing an Open XML doc via styled blocks

I'm working with docx docs, and I need to parse a document into sections on the basis of headings styled with the "heading 1" style. So if I had a doc like this (markup is pseudocode):
<doc>
<title style>Doc Title</title style>
<heading1>First Section</heading1>
...
<heading2>Second Section</heading2>
...
<heading3>Third Section</heading3>
...
</doc>
I'd want to break this into a doc with four sections, the first being the content that precedes the first section. I figure that this is probably pretty simple once you're familiar with Open XML, but I am not.
TIA.
Wow...not even any views on this question all day. Well, I figured it out and thought I'd share the wealth. I can't share the code directly, but it's just three nested loops, one looping through the paragraphs, then the paragraph runs, then the styles. The XPath for each of those is:
.//w:p
./w:pPr
./w:pStyle
Once you find a run with the style you like, you pop back up a level to get the first run, which will contain the styled text. From there on, it's just Comp Sci 101 stuff. I think the real breakthrough was to not even try to mess with the Open Xml SDK (aside from the IO Packaging stuff), and go straight to XML manipulation.

C#-Sorting XML elements -Possible?(without ADO.NET)

Just i need to recreate a xml file after appling sorting on key filed element(say EmpID),The thing is ,i should not use ADO.NET.Which is the best sort to go ahead ?.To do so,What XML Class do i need to use?,LINQ is quite handy?
No need for c\ to do this. you can do it via an XSL file
<xsl:template match="/">
<xsl:apply-template select="yourlementnode">
<xsl:sort select="EmpID" order="ascending" />
</xsl:apply-template>
</xsl:template>
LINQ to XML would probably be your best bet. You could either move the elements "in place" or (possibly more easily) create a new document with the re-ordered elements.
If you can give us some sample XML (input and desired output) it should be fairly easy to come up with some example code.

Categories

Resources