我有一个名为“fragment.xml”的xml文件的一小部分,如下所示,每个“SteamStuff”都有自己的“AttributeValue”值。所以如果我点击一个复制按钮,我需要创建一个小的xml文件只包含名为“pirate”的SteamStuff数据。为此,我创建了包含两个名为“复制”和“粘贴”按钮的窗体。如果我单击“复制”按钮,它应该找到它所在的“fragment.xml”在我的PC中,一旦发现将有一个与它相关联的guid (<Text>pirate</Text>)
,就需要找到名为“pirate”<ID>b6792ed8-680b-4117-a695-9f7ef2c7752b</ID>
的托管对象,所以无论这个ID出现,它应该选择和数据并创建小的xml名为“test.xml
”的文件位于“fragment.xml
”的同一位置。所以test.xml只包含“盗版”的数据
我可以在button1_Click()事件
<?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>
-------------
答案 0 :(得分:1)
使用这些xml扩展程序:http://searisen.com/xmllib/extensions.wiki
创建类,因为它可以更容易地看到它的工作原理:
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");
修改:扩展文件会调整“默认”命名空间。