views:

78

answers:

1

Hello,

here is my scenario,

  1. I copy XML Files from the Original Directory to the Target Directory
  2. I modify the file's attributes in the Original Directory
  3. I compare the modified file's attribute(from the Original Directory) with the copied file's attribute (in the Target Directory) They shouldn't be the same of course

My problem is that NOT all XML Files have the same attributes.

I tried adding a common attribute and modify it but the files are retrieved from a DB and each attribute represents a coloumn there and I can't modify the DB

I'm thinking of looping through the attributes, till I reach the first "non-fixed" attribute and modify it. Is that possible? if so how? and if there is other solutions I'll appreciate it even more :)

Here is an example of part of my xmls

<CtApproachTypes 
DataclassId="1992A9CE-B048-4676-BFD4-FD81F1A65401" 
EntityId="1992A9CE-B048-4676-BFD4-FD81F1A65401" 
Name="PAR" 
Remark="No Remarks"/>
</CT_ApproachTypes>

<MiMissions 
DataclassId="C196A66B-4FA1-461C-9EEF-95A4F2085051" 
EntityId="C196A66B-4FA1-461C-9EEF-95A4F2085051" 
MissionName="Standard"
isib="1" 
</MiMissions>

<StSituations 
DataclassId="679FAC3C-C9EF-41FD-9A13-957915605F01" 
EntityId="679FAC3C-C9EF-41FD-9A13-957915605F01" 
SitName="Standard" 
Status="C" 
Template="1">
</StSituations>

I wanna skip the first two attributes and modify the first attribute after them.

Note: I checked the XML Files and all of them seem to have an attribute with "-name" in them. I was thinking of using "like" to query.. but a more solid solution would be to modify the first attribute after the first 2.

Thanks

A: 

If what you're trying to do is update attributes of XML this could be one way:

XElement elem = XElement.Load("xmlFilePath");
System.Xml.XPath.Extensions.XPathSelectElement(elem, "xpath").SetAttributeValue("attributeName", "newValue");

where xmlFilePath is the path to the XML you're changing, xpath is the path to the element of the xml and attributeName is the name of the nodes attribute you're altering... Ofcourse, you have to add a custom node with attribute to every xml you're using.

EDIT: Since you said you can't modify the XML files in both locations, than all you're left with is to compare the FileInfo properties of the files themselves.

And if the FileInfo properties are not what you need, then there's no other solution but to create a tracking db or a file, which will contain attribute values for your Original and Target directory, by which you can compare and do appropriate action.

For example, if your Original and Target maps 1 on 1, than you could have a simple table structure like this 'TableName, OriginalValue, TargetValue'. Where you'd keep this 'tracking' data (db, xml file, other...) depends on the number of entries, and your current architecture...

EDIT2: If you wan't to append to the first attribute found you can use this:

    XElement elem = XElement.Load("myfilepath.xml");
    elem.FirstAttribute.SetValue(elem.FirstAttribute.Value + "|myothervalue");
veljkoz
I can't add a custom node that's the problem. The XMLs are generated from DB Coloumns representing their attributes. And I can't modify the DB. What I need is a way to modify the XML Files knowing that not all XML Files have a common Attribute.
Reda
You can add attribute at root element because the root always exists and it's not related to tables (if that's not the case, please post a simple xml example of your file).If the attribute doesn't exist the SetAttributeValue will create it for you - just make sure it's some unique name just in case.
veljkoz
I used the "SetAttributeValue" and added a new attribute named "Sabassi" but it still gave me the NullReferenceException.. :S
Reda
Okay so I just realized that i can't add a new attribute. Since I'm gonna compare the XML file with the other (copied) which won't have the newly added attribute (that's why I've been getting the NullException every time I try to compare the 2 xml files). So What I need to do now is simply modify an already existing attribute in the original xml so that it would be valid for comparison when I compare it with the copied file. But then we return to the same problem, that not all XMLs have a common attribute... Anyway of solving this?
Reda
No it has to be through Attributes Comparison, not the whole XML. I'm looking into FirstOrDefault() function now but I keep reaching a dead end. It is somehow what I need. That is to loop through the Attributes of the XML file, find the first attribute that accepts modifying (not with a fixed value) then modify it. And I can then apply this loop on the copied xml files and will return the same attribute. Can you help me with this in any possible way?
Reda
that's really helpful. Sort of what I wanted. It worked (gave me no errors) but it didn't change the value either since the first attribute is DataClassId (fixed) the 2nd is also fixed (EntityId). But what I need now is to write a condition that would skip these and jump right to the 3rd attribute. If you could also tell me of a way to retrieve the change attribute for testing purposes I would be thankful
Reda