Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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中的XmlElement_C#_Xml_Winforms - Fatal编程技术网

C# 修改现有XML中的XmlElement

C# 修改现有XML中的XmlElement,c#,xml,winforms,C#,Xml,Winforms,我在运行时使用以下代码编写xml if (fileDialog.FileName != "" && resultSaveDialog == System.Windows.Forms.DialogResult.OK) { fileDialog.OpenFile(); XDocument Xdoc = new XDocument(new XElement("LinkAnalysis"));

我在运行时使用以下代码编写xml

if (fileDialog.FileName != "" && resultSaveDialog == System.Windows.Forms.DialogResult.OK)
            {
                fileDialog.OpenFile();
                XDocument Xdoc = new XDocument(new XElement("LinkAnalysis"));
                if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "LA_img\\LAIMG.xml"))
                    Xdoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "LA_img\\LAIMG.xml");
                else
                {
                    Xdoc = new XDocument();
                    XElement xmlstart = new XElement("LinkAnalysis");
                    Xdoc.Add(xmlstart);
                }
                XElement xml = new XElement("ImgInfo",
                  new XElement("Number", selectedvertex.Name),
                  new XElement("ImgPath", AppDomain.CurrentDomain.BaseDirectory + "LA_img\\" + fileDialog.SafeFileName));                    
                if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "VerbaLinks.xml"))
                {
                    DataSet dsXML = new DataSet();
                    bool chkName = false;
                    dsXML.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "LA_img\\LAIMG.xml");
                    try
                    {
                        // Here i am checking if number is already exist in xml
                        chkName = dsXML.Tables["ImgInfo"].AsEnumerable().Any(s => s.Field<string>("Number").ToLower() == selectedvertex.Name);
                       // If exist, I wanna update that element with new ImgPath.
                    }
                    catch
                    {
                        chkName = false;
                    }
                }

                if (Xdoc.Descendants().Count() > 0)
                    Xdoc.Descendants().First().Add(xml);
                else
                {
                    Xdoc.Add(xml);
                }
                Xdoc.Element("LinkAnalysis").Save(AppDomain.CurrentDomain.BaseDirectory + "LA_img\\LAIMG.xml");
if(fileDialog.FileName!=“&&resultSaveDialog==System.Windows.Forms.DialogResult.OK)
{
fileDialog.OpenFile();
XDocument Xdoc=新XDocument(新XElement(“链接分析”);
如果(System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory+“LA_img\\LAIMG.xml”))
Xdoc=XDocument.Load(AppDomain.CurrentDomain.BaseDirectory+“LA_img\\LAIMG.xml”);
其他的
{
Xdoc=新的XDocument();
XElement xmlstart=新XElement(“链接分析”);
Xdoc.Add(xmlstart);
}
XElement xml=新XElement(“ImgInfo”,
新元素(“编号”,selectedvertex.Name),
新的XElement(“ImgPath”,AppDomain.CurrentDomain.BaseDirectory+“LA_img\\”+fileDialog.SafeFileName));
if(File.Exists(AppDomain.CurrentDomain.BaseDirectory+“orarinks.xml”))
{
数据集dsXML=新数据集();
bool-chkName=false;
ReadXml(AppDomain.CurrentDomain.BaseDirectory+“LA_img\\LAIMG.xml”);
尝试
{
//这里我检查xml中是否已经存在数字
chkName=dsXML.Tables[“ImgInfo”].AsEnumerable().Any(s=>s.Field(“Number”).ToLower()==selectedvertex.Name);
//如果存在,我想用新的ImgPath更新该元素。
}
抓住
{
chkName=false;
}
}
如果(Xdoc.subjects().Count()>0)
Xdoc.subjects().First().Add(xml);
其他的
{
Add(xml);
}
Xdoc.Element(“LinkAnalysis”).Save(AppDomain.CurrentDomain.BaseDirectory+“LA_img\\LAIMG.xml”);
在这里,在xml中添加新元素之前,我检查它是否已经存在于xml中,如果是,我只想更新相应数字的ImgPath属性

下面是示例xml

<?xml version="1.0" encoding="utf-8"?>
<LinkAnalysis>
<ImgInfo>
  <Number>xyz</Number>
  <ImgPath>D:\Projects\VERBALinks\VERBALinks\bin\Debug\LA_img\grid.png</ImgPath>
</ImgInfo>
<ImgInfo>
  <Number>pqr</Number>
  <ImgPath>D:\Projects\VERBALinks\VERBALinks\bin\Debug\LA_img\1367408385_Social network.png</ImgPath>
</ImgInfo>
<ImgInfo>
  <Number>wxy</Number>
  <ImgPath>D:\Projects\VERBALinks\VERBALinks\bin\Debug\LA_img\Link_Analysis.png</ImgPath>
</ImgInfo>  
</LinkAnalysis>

xyz
D:\Projects\veralinks\veralinks\bin\Debug\LA\u img\grid.png
pqr
D:\Projects\orarlinks\orarlinks\bin\Debug\LA\u img\1367408385\u Social network.png
wxy
D:\Projects\oraryinks\oraryinks\bin\Debug\LA\u img\Link\u Analysis.png

您正在使用LINQ to XML。您可以轻松地使用LINQ而无需使用
数据集
。我建议您阅读MSDN上关于LINQ to XML的文档。它将清楚地描述如何浏览XML文档和修改部分。