How To change XML namespace of certain element - c#

I have some set of xml generated via xmlserialization of some WCF messages.
Now I want to make a generic method in which I will provide an xml filename and a prefix like mailxml12.
Then in xml file those elements that don't have any namespace prefix in their name should be replaced with mailxml12:
Like source file is:
<DeliveryApptCreateRequest d2p1:ApptType="Pallet" d2p1:PickupOrDelivery="Delivery" d2p1:ShipperApptRequestID="4490660303D5" d2p1:SchedulerCRID="234234" xmlns:d2p1="http://idealliance.org/Specs/mailxml12.0a/mailxml_defs" xmlns="http://idealliance.org/Specs/mailxml12.0a/mailxml_tm">
<SubmittingParty d2p1:MailerID6="123446" d2p1:CRID="342343" d2p1:MaildatUserLicense="A123" />
<SubmittingSoftware d2p1:SoftwareName="asds" d2p1:Vendor="123" d2p1:Version="12" />
<SubmitterTrackingID>2CAD3F71B4405EB16392</SubmitterTrackingID>
<DestinationEntry>No</DestinationEntry>
<OneTimeAppt>
<PreferredAppt>2012-06-29T09:00:00Z</PreferredAppt>
</OneTimeAppt>
<TrailerInfo>
<Trailer>
<TrailerNumber>A</TrailerNumber>
<TrailerLength>20ft</TrailerLength>
</Trailer>
<Carrier>
<CarrierName>N/A</CarrierName>
<URL>http://test.com</URL>
</Carrier>
<BillOfLadingNumber>N/A</BillOfLadingNumber>
</TrailerInfo>
</DeliveryApptCreateRequest>
After the desired method it should be changed into all element name which doesn't have prefix with mailxml:.
Like DeliveryApptCreateRequest should become mailxml:DeliveryApptCreateRequest
while element like d2p1:CompanyName should remain as it is.
I have tried with following code
private void RepalceFile(string xmlfile)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlfile);
var a = doc.CreateAttribute("xmlns:mailxml12tm");
a.Value = "http://idealliance.org/Specs/mailxml12.0a/mailxml_tm";
doc.DocumentElement.Attributes.Append(a);
doc.DocumentElement.Prefix = "mailxml12tm";
foreach (XmlNode item in doc.SelectNodes("//*"))
{
if (item.Prefix.Length == 0)
item.Prefix = "mailxml12tm";
}
doc.Save(xmlfile);
}
only problem with it is that root element remain as it is while all are changed as i needed

You can just parse the whole XML as a string and insert namespaces where appropriate. This solution, however, can create lots of new strings only used within the algorithm, which is not good for the performance. However, I've written a function parsing it in this manner and it seems to run quite fast for sample XML you've posted ;). I can post it if you would like to use it.
Another solution is loading XML as XmlDocument and taking advantage of the fact it's a tree-like structure. This way, you can create a method recursively adding appropriate namespaces where appropriate.
Unfortunately, XmlNode.Name attribute is read-only and that's why you have to manually copy the entire structure of the xml to change names of some nodes.
I don't have time to write the code right now, so I just let you write it. If you encounter any issues with it, just let me know.
Update
I've tested your code and code suggested by Jeff Mercado and both of them seem to work correctly, at least in the sample XML you've posted in the question. Make sure the XML you are trying to parse is the same as the one you've posted.
Just to make it work and solve adding namespace issue originally asked, you can use the code, which handles the whole XML as a String and parses it manually:
private static String UpdateNodesWithDefaultNamespace(String xml, String defaultNamespace)
{
if (!String.IsNullOrEmpty(xml) && !String.IsNullOrEmpty(defaultNamespace))
{
int currentIndex = 0;
while (currentIndex != -1)
{
//find index of tag opening character
int tagOpenIndex = xml.IndexOf('<', currentIndex);
//no more tag openings are found
if (tagOpenIndex == -1)
{
break;
}
//if it's a closing tag
if (xml[tagOpenIndex + 1] == '/')
{
currentIndex = tagOpenIndex + 1;
}
else
{
currentIndex = tagOpenIndex;
}
//find corresponding tag closing character
int tagCloseIndex = xml.IndexOf('>', tagOpenIndex);
if (tagCloseIndex <= tagOpenIndex)
{
throw new Exception("Invalid XML file.");
}
//look for a colon within currently processed tag
String currentTagSubstring = xml.Substring(tagOpenIndex, tagCloseIndex - tagOpenIndex);
int firstSpaceIndex = currentTagSubstring.IndexOf(' ');
int nameSpaceColonIndex;
//if space was found
if (firstSpaceIndex != -1)
{
//look for namespace colon between tag open character and the first space character
nameSpaceColonIndex = currentTagSubstring.IndexOf(':', 0, firstSpaceIndex);
}
else
{
//look for namespace colon between tag open character and tag close character
nameSpaceColonIndex = currentTagSubstring.IndexOf(':');
}
//if there is no namespace
if (nameSpaceColonIndex == -1)
{
//insert namespace after tag opening characters '<' or '</'
xml = xml.Insert(currentIndex + 1, String.Format("{0}:", defaultNamespace));
}
//look for next tags after current tag closing character
currentIndex = tagCloseIndex;
}
}
return xml;
}
You can check this code out in order to make you app working, however, I strongly encourage you to determine why the other solutions suggested didn't work.

Since in this case you have a default namespace defined, you could just remove the default namespace declaration and add a new declaration for your new prefix using the old namespace name, effectively replacing it.
var prefix = "mailxml";
var content = XElement.Parse(xmlStr);
var defns = content.GetDefaultNamespace();
content.Attribute("xmlns").Remove();
content.Add(new XAttribute(XNamespace.Xmlns + prefix, defns.NamespaceName));

#JeffMercado's solution didn't work for me (probably since I didn't have a default namespace).
I ended up using:
XNamespace ns = Constants.Namespace;
el.Name = (ns + el.Name.LocalName) as XName;
To change the namespace of a whole document I used:
private void rewriteNamespace(XElement el)
{
// Change namespace
XNamespace ns = Constants.Namespace;
el.Name = (ns + el.Name.LocalName) as XName;
if (!el.HasElements)
return;
foreach (XElement d in el.Elements())
rewriteNamespace(d);
}
Usage:
var doc = XDocument.parse(xmlStr);
rewriteNamespace(doc.Root)
HTH

Related

Edit and save XML adding extra [] characters

I have a requirement to edit and save- precisely replace a text in an xml (within the c# project) with a value from arguments and save in temp location. The value is replaced and saved in location, but it adds some characters- [] and hence when i use the xml in another application as input, it is shown as incorrect xml! Even when i remove the extra character and save and rerun it shows the same error. However when i remove the extra character and paste the whole xml into a new file it works fine! I dont understand whats the issue. Have pasted my code below:
{
parameterFileName = "test";
tempPath = Path.GetTempPath() + parameterFileName + DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss") + ".xml";
XmlDocument xdoc = GetParameterXML(parameterFileName);
XmlNode root = xdoc.DocumentElement;
XmlNode node = xdoc.DocumentElement.SelectSingleNode(#"/root/inputParameters");
XmlNode childNode = node.ChildNodes[0];
if (childNode is XmlCDataSection)
{
XmlCDataSection cdataSection = childNode as XmlCDataSection;
if (cdataSection.Value.Contains("ID_VALUE"))
{
cdataSection.Value = cdataSection.Value.Replace("ID_VALUE", id);
}
}
xdoc.Save(tempPath);
}
public static XmlDocument GetParameterXML(string parameterFileName)
{
var sDllPath = AppDomain.CurrentDomain.BaseDirectory;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(sDllPath + "\\Templates\\" + parameterFileName + ".xml");
return xDoc;
}
When you parse Xml Document by using XmlDocument with DTD then empty Internal Subset means Square Brackets [] is automatically inserted.
public static XmlDocument GetParameterXML(string parameterFileName)
{
var sDllPath = AppDomain.CurrentDomain.BaseDirectory;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(sDllPath + "\\Templates\\" + parameterFileName + ".xml");
if (xDoc.DocumentType != null)
{
var name = xDoc.DocumentType.Name;
var publicId = xDoc.DocumentType.PublicId;
var systemId = xDoc.DocumentType.SystemId;
var parent = xDoc.DocumentType.ParentNode;
var documentTypeWithNullInternalSubset = xDoc.CreateDocumentType(name, publicId, systemId, null);
parent.ReplaceChild(documentTypeWithNullInternalSubset, xDoc.DocumentType);
}
return xDoc;
}
Does it matter?
No this does not matter. but its a well formed XML if your XML doesn't contain any internal subset then it represent as blank square brackets []. it means that your xml doesn't contain any internal subset.
While parsing xml with XDocument with no internal subset then XDocument append blank square brackets [] instead of display nothing in DOCTYPE.
What does an empty internal subset do?
The basic purpose of an internal entity is to get rid of typing same content (like the name of the organization) again and again. And instead, we can define an internal entity to contain the text and then only you need to use the entity where you want to insert the text. Because the entity is expanded by the parser, you can be assured that you'll get the same text in every location. The parser will also catch if you misspell an entity name.
You can read more about Internal Subset here

Overloading namespaces in xml with envelope

I have a program that iterates through XML Documents to search for values.
I want to extract the version number of the document I found and add it to the search result.
The problem I encounter is, that the documents have a lot of different namespaces, and most of them have an envelope. The envelope has the same namespaceprefix like the divergent namespace in the payload of the document.
I am struggeling to extract values from the payload, because I am not able to get the namespace of the inner document.
if (myX != null)
{
XNamespace myNs;
if (myX.Root.FirstAttribute == null)
myNs = "";
else
{
myNs = myX.Root.FirstAttribute.Value;
var dummy2 = myX.Root.Descendants("Message").First().FirstAttribute;
var dummy1 = myX.Root.Descendants(myNs + "Message").First().FirstAttribute;
}
foreach (XNode xN in myX.Root.DescendantNodes())
{
if (xN is XElement)
{
if (!wildcard)
{
if (((XElement)xN).Value == value)
{
myResultSet[0].Add(new ResultSet(file, myX.Root.Descendants(myNs + "MessageVersion").First().Value));
}
}
else if (wildcard)
{
if (((XElement)xN).Value.Contains(value))
{
myResultSet[0].Add(new ResultSet(file, myX.Root.Descendants(myNs + "MessageVersion").First().Value));
}
}
}
}
}
I can get the ns0:Body Element, but I fail to get anything with the "http://dummy.schema.com/ss2/schemas/2.5/DESADV" namespace.
The XML is something like this:
<ns0:Envelope xmlns:ns0="http://dummy.schema.com/ss2/schemas/2.3">
<ns0:SenderILN>123456789123</ns0:SenderILN>
<ns0:ReceiverILN>987654321987</ns0:ReceiverILN>
<ns0:EnvelopeNumber>12345</ns0:EnvelopeNumber>
<ns0:Body>
<ns0:Message xmlns:ns0="http://dummy.schema.com/ss2/schemas/2.5/DESADV">
<ns0:Header>
<ns0:MessageType>DESADV</ns0:MessageType>
<ns0:SenderILN>123456789123</ns0:SenderILN>1
<ns0:ReceiverILN>987654321987</ns0:ReceiverILN>
<ns0:MessageVersion>2.5</ns0:MessageVersion>
</ns0:Header>
<ns0:DESADV>
<ns0:Payload />
</ns0:DESADV>
</ns0:Message>
</ns0:Body>
</ns0:Envelope>
Is there a possibility to get to the inner message without removing the envelope first?
I'm not sure why you're trying to 'extract' the namespace. The namespace is part of the element name - just as you know the local name MessageVersion you should also know its namespace.
You should be doing something like this:
XNamespace ns = "http://dummy.schema.com/ss2/schemas/2.5/DESADV"
var version = (string) doc
.Descendants(ns + "MessageVersion")
.Single();
If for some reason you don't know or don't care about the namespace, you can just search by local name:
var version = (string) doc
.Descendants()
.Single(x => x.Name.LocalName == "MessageVersion");
if (((XElement)xN).Value == value)
{
XNamespace myNs = ((XElement)xN).Name.Namespace;
myResultSet[0].Add(new ResultSet(file, myX.Root.Descendants(myNs + "MessageVersion").First().Value));
}
After starting from the result and not the document itself it was quite easy to extract the namespace and the value.
But this works only, because I am brute forcing the document. I found no solution to do it in a sober way... Still looking for a better solution.

Change XML node with same name?

everyone!
I have an XML file and need to change the value of a node, specifically the indicated line. The problem i have is that as you can see, there are many nodes.
How can i change this line? This XML file could be much larger, so i am looking for a solution that would take different amounts of 'launch.file' nodes into account.
The node that will need to be set to True will be identified by the corresponding NAME tag. So if i typed in ULTII, the DISABLED node for that block will be set to True. If i typed in Catl, then the DISABLED node for that block would be changed.
<?xml version="1.0" encoding="windows-1252"?>
<SBase.Doc Type="Launch" version="1,0">
<Descr>Launch</Descr>
<Filename>run.xml</Filename>
<Disabled>False</Disabled>
<Launch.ManualLoad>False</Launch.ManualLoad>
<Launch.File>
<Name>Catl</Name>
<Disabled>False</Disabled>
<ManualLoad>False</ManualLoad>
<Path>ft\catl\catl.exe</Path>
</Launch.File>
<Launch.File>
<Disabled>False</Disabled> <!-- change to True -->
<ManualLoad>False</ManualLoad>
<Name>ULTII</Name>
<Path>F:\ULTII.exe</Path>
<NewConsole>True</NewConsole>
</Launch.File>
<Launch.File>
<Name>ECA</Name>
<Disabled>False</Disabled>
<Path>C:\ECA.exe</Path>
</Launch.File>
</SBase.Doc>
I am using Visual Studio 2012, should you need to know.
Thank you to anyone who can help me out on this, i really appreciate it.
Heres my method to do what you want
private void DisableLaunchFile(string xmlfile, string launchFileName){
XDocument doc = XDocument.Load(xmlfile);
var launchFileElement = doc.Descendants("Launch.File").Where (d => d.Element("Name").Value == lauchFileName);
launchFileElement.Elements("Disabled").First().Value = true.ToString();
doc.Save(xmlfile);
}
Use it like:
string pathToXmlFile = //assign ;
DisableLaunchFile(pathToXmlFile, "Catl");
DisableLaunchFile(pathToXmlFile, "ULTII");
This can be achieved by using LINQ to XML (see XDocument Class).
Assuming that there is the single Launch.File element with Name element with value "ULTII":
var document = XDocument.Load(...);
var ultiiElement = document
.Descendants("Launch.File")
.Single(fileElement => fileElement.Element("Name").Value == "ULTII");
ultiiElement.Element("Disabled").Value = "True"; // or true.ToString()
document.Save(...);
This method will do the trick:
public void ChangeNode(string name, string filePath)
{
XDocument xDocument;
using (var streamReader = new StreamReader(filePath))
{
xDocument = XDocument.Parse(streamReader.ReadToEnd());
}
var nodes = xDocument.Descendants("Launch.File");
foreach (var node in nodes)
{
var nameNode = node.Descendants("Name").FirstOrDefault();
if (nameNode != null && nameNode.Value == name)
{
var disabledNode = node.Descendants("Disabled").FirstOrDefault();
if (disabledNode != null)
{
disabledNode.SetValue("True");
}
}
}
using (var streamWriter = new StreamWriter(filePath))
{
xDocument.Save(streamWriter);
}
}
The name you want to pass in is the name of the node that you want to change and the path is the file path to the xml file. So you might call it like:
ChangeNode("ULTII", "C:\\output.xml");
You may need to tidy this up a bit like matching the node name invariant of case or culture but it should get you started.

Relative references in an XML document

I am trying to pull out data from an XML document that seems to use relative references like this:
<action>
<topic reference="../../action[110]/topic"/>
<context reference="../../../../../../../../../../../../../contexts/items/context[2]"/>
</action>
Two questions:
Is this normal or common?
Is there a way to handle this with linq to XML / XDocument or would I need to manually traverse the document tree?
Edit:
To clarify, the references are to other nodes within the same XML document. The context node above references a list of contexts, and says to get the one at index 2.
The topic node worries me more because it's referencing a certain other action's topic, which could in turn reference a list of topics. If that wasn't happening I would have just loaded the lists of contexts and topics in a cache and looked them up that way.
You can use XPATH Query to extract the nodes and it is very efficient.
Step1: Load the XML into XMLDocument
Step2: use node.SelectNodes("//*[reference]")
Step3: After that you can loop through the XML nodes.
I ended up manually traversing the tree. But with extension methods it's all nice and out of the way. In case it might help anyone in the future, this is what I threw together for my use-case:
public static XElement GetRelativeNode(this XAttribute attribute)
{
return attribute.Parent.GetRelativeNode(attribute.Value);
}
public static string GetRelativeNode(this XElement node, string pathReference)
{
if (!pathReference.Contains("..")) return node; // Not relative reference
var parts = pathReference.Split(new string[] { "/"}, StringSplitOptions.RemoveEmptyEntries);
XElement current = node;
foreach (var part in parts)
{
if (string.IsNullOrEmpty(part)) continue;
if (part == "..")
{
current = current.Parent;
}
else
{
if (part.Contains("["))
{
var opening = part.IndexOf("[");
var targetNodeName = part.Substring(0, opening);
var ending = part.IndexOf("]");
var nodeIndex = int.Parse(part.Substring(opening + 1, ending - opening - 1));
current = current.Descendants(targetNodeName).Skip(nodeIndex-1).First();
}
else
{
current = current.Element(part);
}
}
}
return current;
}
And then you'd use it like this (item is an XElement):
item.Element("topic").Attribute("reference").GetRelativeNode().Value

How to replace all "values" in an XML document with "0.0" using C# (preferably LINQ)?

This is not a homework; I need this for my unit tests.
Sample input: <rows><row><a>1234</a><b>Hello</b>...</row><row>...</rows>.
Sample output: <rows><row><a>0.0</a><b>0.0</b>...</row><row>...</rows>.
You may assume that the document starts with <rows> and that parent node has children named <row>. You do not know the name of nodes a, b, etc.
For extra credit: how to make this work with an arbitrary well-formed, "free-form" XML?
I have tried this with a regex :) without luck. I could make it "non-greedy on the right", but not on the left. Thanks for your help.
EDIT: Here is what I tried:
private static string ReplaceValuesWithZeroes(string gridXml)
{
Assert.IsTrue(gridXml.StartsWith("<row>"), "Xml representation must start with '<row>'.");
Assert.IsTrue(gridXml.EndsWith("</row>"), "Xml representation must end with '<row>'.");
gridXml = "<deleteme>" + gridXml.Trim() + "</deleteme>"; // Fake parent.
var xmlDoc = XDocument.Parse(gridXml);
var descendants = xmlDoc.Root.Descendants("row");
int rowCount = descendants.Count();
for (int rowNumber = 0; rowNumber < rowCount; rowNumber++)
{
var row = descendants.ElementAt(0);
Assert.AreEqual<string>(row.Value /* Does not work */, String.Empty, "There should be nothing between <row> and </row>!");
Assert.AreEqual<string>(row.Name.ToString(), "row");
var rowChildren = row.Descendants();
foreach (var child in rowChildren)
{
child.Value = "0.0"; // Does not work.
}
}
// Not the most efficient but still fast enough.
return xmlDoc.ToString().Replace("<deleteme>", String.Empty).Replace("</deleteme>", String.Empty);
}
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlElement el in doc.SelectNodes("//*[not(*)]"))
el.InnerText = "0.0";
xml = doc.OuterXml;
or to be more selective about non-empty text nodes:
foreach (XmlText el in doc.SelectNodes("//text()[.!='']"))
el.InnerText = "0.0";
XDocument xml = XDocument.Load(myXmlFile);
foreach (var element in xml.Descendants("row").SelectMany(r => r.Elements()))
{
element.Value = "0.0";
}
Note that this general search for "Desscendants('row')" is not very efficient--but it satisfies the 'arbitrary format' requirement.
You should take look at HTML Agility Pack. It allows you to treat html documents as well-formed xml's, therefore you can parse it and change values.
I think you can use Regex.Replace method in C#. I used the below regex to replace all the XML elements values:
[>]+[a-zA-Z0-9]+[<]+
This will basically match text starting with a '>'{some text alphabets or number}'<'.
I was able to use this successfully in Notepad++. You can write a small program as well using this.

Categories