Reputation: 1
I have a small part of a xml file named "fragment.xml" which is shown below where each "SteamStuff" has their own "AttributeValue" value .So here if i click a copy button, i need to create small xml file contains only the datas of SteamStuff named "pirate".For this i have created windows form containing two buttons named "Copy" and "Paste".If i click "Copy" button it should find "fragment.xml" where it is located in my PC and need to find the managed object named "pirate" (<Text>pirate</Text>)
once it finds there will be a guid <ID>b6792ed8-680b-4117-a695-9f7ef2c7752b</ID>
assosiated with it ,so whereever this ID appears it should select and datas and create small xml file named "test.xml
" on the same location of "fragment.xml
" . So test.xml contains only datas of "pirate"
What code i can implement here in button1_Click() event
<?xml version="1.0" standalone="yes"?>
<LeafDataSchema xmlns="http://tempuri.org/LeafDataSchema.xsd">
<SteamStuff>
<ID>b6792ed8-680b-4117-a695-9f7ef2c7752b</ID>
<Label>pirate</Label>
<Owner>00000000-0000-0000-0000-000000000000</Owner>
<Image>00000000-0000-0000-0000-000000000000</Image>
<ClassID>00000000-0000-0000-0000-000000000008</ClassID>
<DefaultApp>00000000-0000-0000-0000-000000000000</DefaultApp>
<Name>pirate</Name>
<Volatile>true</Volatile>
</SteamStuff>
<AttributeValue>
<ID>2977f4e0-84ab-4ad2-8c4d-6bcb49727889</ID>
<ObjectID>b6792ed8-680b-4117-a695-9f7ef2c7752b</ObjectID>
<Text>True</Text>
<AttributeName>Monitor</AttributeName>
<ClassID>00000000-0000-0000-0000-000000000008</ClassID>
</AttributeValue>
-------------
Upvotes: 0
Views: 333
Reputation: 11955
Using these xml extensions: http://searisen.com/xmllib/extensions.wiki
Creating classes, because it makes it easier to see how it works:
public class XFile
{
XElement self;
public XFile(string file)
{
self = XElement.Load(file);
}
public XNamespace Namespace
{
get { return _Namespace ?? (_Namespace = self.GetDefaultNamespace()); }
}
XNamespace _Namespace;
public AttributeValue[] AttributeValues
{
get
{
return _AttributeValues ??
(_AttributeValues = self.GetEnumerable("AttributeValue", x => new AttributeValue(this, x)).ToArray());
}
}
AttributeValue[] _AttributeValues;
public SteamStuff[] SteamStuffs
{
get
{
return _SteamStuffs ??
(_SteamStuffs = self.GetEnumerable("SteamStuff", x => new SteamStuff(this, x)).ToArray());
}
}
SteamStuff[] _SteamStuffs;
}
public class SteamStuff
{
XElement self;
XFile parent;
public SteamStuff(XFile parent, XElement self)
{
this.parent = parent;
this.self = self;
}
public XElement Element { get { return self; } }
public string ID
{
get { return self.Get("ID", string.Empty); }
}
public string Label
{
get { return self.Get("Label", string.Empty); }
}
}
public class AttributeValue
{
XElement self;
XFile parent;
public AttributeValue(XFile parent, XElement self)
{
this.parent = parent;
this.self = self;
}
public XElement Element { get { return self; } }
public string ObjectID
{
get { return self.Get("ObjectID", string.Empty); }
}
}
XFile xfile = new XFile("test.xml");
SteamStuff[] pirates = xfile.SteamStuffs
.Where(steam => steam.Label == "pirate")
.ToArray();
AttributeValue[] associated = xfile.AttributeValues
.Where(av => pirates.Any(pirate => pirate.ID == av.ObjectID))
.ToArray();
// write to new file
XElement fragments = new XElement(xfile.Namespace + "fragments");
foreach(SteamStuff steamStuff in pirates)
fragments.Add(steamStuff.Element);
foreach(AttributeValue value in associated)
fragments.Add(value.Element);
fragments.Save("fragment_file.xml");
Edit: Extensions file adjusts for the 'default' namespace.
Upvotes: 1