Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在没有根元素的情况下编写片段化的xml文件?_C#_Xml - Fatal编程技术网

C# 如何在没有根元素的情况下编写片段化的xml文件?

C# 如何在没有根元素的情况下编写片段化的xml文件?,c#,xml,C#,Xml,我得到了碎片化的xml文件(没有根元素/节点的文档): 稍后,我更改…部分并获取以下数据: <?xml version="1.0" standalone="no" ?> <WndPos name="Login" l="703" r="1264" t="323" b="909" /> <WndPos name="Main" l="703" r="768" t="323" b="609" /> <LayerManager /> <ViewLaye

我得到了碎片化的xml文件(没有根元素/节点的文档):

稍后,我更改
部分并获取以下数据:

<?xml version="1.0" standalone="no" ?>
<WndPos name="Login" l="703" r="1264" t="323" b="909" />
<WndPos name="Main" l="703" r="768" t="323" b="609" />
<LayerManager />
<ViewLayers name="Roof" roof="1">
    <Layer level="1" visible="1" />
</ViewLayers>
<DirProfiles>
    <ProfileInfo ProfileName="Control1" DatabasePath="D:\Database\Control1" />
    <ProfileInfo ProfileName="Control2" DatabasePath="D:\Database\Control2" />
    <ProfileInfo ProfileName="Control3" DatabasePath="D:\Database\Control3" />
    <ProfileInfo ProfileName="Control4" DatabasePath="D:\Database\Control4" />
    <ProfileInfo ProfileName="Control5" DatabasePath="D:\Database\Control5" />
</DirProfiles>
<DirHistory>
     <ProfileInfo Use="Database" Path="D:\Database\Control" />
</DirHistory>

但它使用根元素保存,而应用程序不能使用此xml。

大多数xml工具和应用程序只能处理格式良好的xml文档(与“片段”不同,正确地说是“外部通用解析实体”)。因此,通过尝试处理和创建片段,您将自己与XML主流割裂开来,并极大地限制了您的工具库。你确定这是明智的吗?那么反过来做同样的事情:迭代元素并分别编写它们。显然,这里的不是XML文档,所以不能将其另存为XML文档。(“碎片化XML”不是标准的某种变体;其结果根本不是正确的XML。不过,它的顶部仍然有一个XML声明,这很有趣。)不写XML的标识(第一行)的最简单方法是不读第一行。因此,在创建XmlReader之前,在streamreader中添加一行:fs.ReadLine()@MichaelKay我是一名测试人员,正在编写一些测试自动化程序,必须处理AUT(测试中的应用程序)中的文件,我必须将此文件作为给定文件处理。我可以更新节,但不能更改整个文件。谢谢。@jdweng我试过你的建议,但没有从文档中删除
。谢谢
string xmlPath = Environment.GetEnvironmentVariable("USERPROFILE") + "\\my.xml";

XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;

XDocument doc = new XDocument(new XElement("root"));
XElement root = doc.Descendants().First();

using (StreamReader fs = new StreamReader(xmlPath))
using (XmlReader xr = XmlReader.Create(fs, settings))
{
    while(xr.Read())
    {
        if (xr.NodeType == XmlNodeType.Element)
        {
            root.Add(XElement.Load(xr.ReadSubtree())); 
        }
     }
}
<?xml version="1.0" standalone="no" ?>
<WndPos name="Login" l="703" r="1264" t="323" b="909" />
<WndPos name="Main" l="703" r="768" t="323" b="609" />
<LayerManager />
<ViewLayers name="Roof" roof="1">
    <Layer level="1" visible="1" />
</ViewLayers>
<DirProfiles>
    <ProfileInfo ProfileName="Control1" DatabasePath="D:\Database\Control1" />
    <ProfileInfo ProfileName="Control2" DatabasePath="D:\Database\Control2" />
    <ProfileInfo ProfileName="Control3" DatabasePath="D:\Database\Control3" />
    <ProfileInfo ProfileName="Control4" DatabasePath="D:\Database\Control4" />
    <ProfileInfo ProfileName="Control5" DatabasePath="D:\Database\Control5" />
</DirProfiles>
<DirHistory>
     <ProfileInfo Use="Database" Path="D:\Database\Control" />
</DirHistory>
doc.Save();