标签: xmldocument

C#XMLDocument到DataTable?

我假设我必须通过DataSet执行此操作,但它不喜欢我的语法.

我有一个名为"XmlDocument xmlAPDP"的XMLDocument.

我希望它在名为"DataTable dtAPDP"的DataTable中.

我还有一个名为"DataSet dsAPDP"的DataSet.

-

如果我做DataSet dsAPDP.ReadXML(xmlAPDP)它不喜欢它,因为ReadXML想要一个字符串,我假设一个文件名?

.net c# datatable xmldocument dataset

12
推荐指数
1
解决办法
5万
查看次数

有没有办法保存XmlDocument*没有*缩进和行返回?

我所有的搜索都让人们提出了相反的问题,但如果用行返回和缩进保存,我有一个文件增长了近50%.

这有什么办法吗?

编辑我不是在谈论打开文件,而是保存文件.此代码为我重现了'bug':

var path = @"C:\test.xml";
System.IO.File.WriteAllText(path, "<root>\r\n\t<line></line>\r\n\t<line></line>\r\n</root>");
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.PreserveWhitespace = false;
doc.Load(path);
doc.PreserveWhitespace = false; //just in case!
doc.Save(path);
Run Code Online (Sandbox Code Playgroud)

中间的断点显示doc.InnerXml是有效的<root><line></line><line></line></root>,正如预期的那样.但最后的内容test.xml是:

<root>
  <line>
  </line>
  <line>
  </line>
</root>
Run Code Online (Sandbox Code Playgroud)

.net c# xmldocument

12
推荐指数
1
解决办法
1万
查看次数

错误: - XmlReader状态应该是XDocument.Load上的Interactive

我收到以下错误: -

System.InvalidOperationException:XmlReader状态应为Interactive.System.Xml.Linq.XDocument.Load(XmlReader reader,LoadOptions选项)中的System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r,LoadOptions o)

在以下代码中.有谁能指出我在这里做错了什么?

static XDocument GetContentAsXDocument(string xmlData)
{
    XmlDocument xmlDocument = new XmlDocument();
    if (!string.IsNullOrEmpty(xmlData))
    {
        xmlDocument.LoadXml(xmlData);
        return xmlDocument.ToXDocument();
    }
    else
    {
        return new XDocument();
    }
}


/// <summary>
///  Converts XMLDocument to XDocument
/// </summary>
/// <param name="xmlDocument"></param>
/// <returns></returns>
public static XDocument ToXDocument( this XmlDocument xmlDocument )
{
    using( var nodeReader = new XmlNodeReader( xmlDocument ) )
    {
        nodeReader.MoveToContent();
        return XDocument.Load(
             nodeReader,
            (LoadOptions.PreserveWhitespace |
             LoadOptions.SetBaseUri |
             LoadOptions.SetLineInfo));
    }
}
Run Code Online (Sandbox Code Playgroud)

c# xml xmldocument linq-to-xml

12
推荐指数
1
解决办法
5101
查看次数

在C#中的节点内搜索的XmlNode.SelectSingleNode语法

我想限制我的搜索子节点在我当前的节点内.例如,我有以下代码:

XmlNodeList myNodes = xmlDoc.DocumentElement.SelectNodes("//Books");
    foreach (XmlNode myNode in myNodes)
    {
         string lastName = "";
         XmlNode lastnameNode = myNode.SelectSingleNode("//LastName");
         if (lastnameNode != null)
         {
              lastName = lastnameNode.InnerText;
         }
    }
Run Code Online (Sandbox Code Playgroud)

我希望从foreach内部的当前myNode中搜索LastName元素.发生的事情是找到的LastName始终来自带有myNodes的第一个节点.我不想硬编码LastName的确切路径,而是允许它灵活地找到myNode的内部.我原以为在myNode上使用SelectSingleNode方法会将搜索限制在myNode的xml内容中,而不包括父节点.

c# xml xmldocument

12
推荐指数
1
解决办法
3万
查看次数

如何将xmldocument保存到流中

我已经编写了代码来解析我的xml文件,XmlReader所以我不想重写它.我现在已经为程序添加了加密功能.我有encrypt()和decrypt()函数,它们采用xml文档和加密算法.我有一个使用xml阅读器来解析文件的函数,但现在使用xml文档我不知道如何创建xmlreader.

问题是如何将我的xml文档保存到流.我确信它很简单,但我对流不了解.

XmlDocument doc = new XmlDocument();
        doc.PreserveWhitespace = true;
        doc.Load(filep);
        Decrypt(doc, key);

        Stream tempStream = null;
        doc.Save(tempStream);   //  <--- the problem is here I think

        using (XmlReader reader = XmlReader.Create(tempStream))  
        {
            while (reader.Read())
            { parsing code....... } }
Run Code Online (Sandbox Code Playgroud)

c# xmldocument xmlreader

12
推荐指数
1
解决办法
5万
查看次数

在C#中解码CDATA部分

我有一点XML如下:

<section>
  <description>
    <![CDATA[
      This is a "description"
      that I have formatted
    ]]>
  </description>
</section>
Run Code Online (Sandbox Code Playgroud)

我正在使用它,curXmlNode.SelectSingleNode("description").InnerText但值返回

\r\n      This is a "description"\r\n      that I have formatted
代替
This is a "description" that I have formatted.

有没有一种简单的方法从CDATA部分获得那种输出?离开实际的CDATA标签似乎让它以同样的方式返回.

.net c# xml xmldocument cdata

11
推荐指数
3
解决办法
4万
查看次数

如何从XmlDocument中使用XML命名空间选择XML节点?

我的代码试图从网站的RSS源中获取数据.它可以很好地抓取节点,但是当尝试从带有冒号的节点中获取数据时,它会崩溃并提供错误"需要命名空间管理器或XsltContext.此查询具有前缀,变量或用户定义的函数." 代码如下所示:

WebRequest request = WebRequest.Create("http://buypoe.com/external.php?type=RSS2&lastpost=true");
WebResponse response = request.GetResponse();

StringBuilder sb = new StringBuilder("");
System.IO.StreamReader rssStream = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));

XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);

XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");

for (int i = 0; i < 5; i++)
{
   XmlNode rssDetail;
   rssDetail = rssItems.Item(i).SelectSingleNode("dc:creator");

   if (rssDetail != null)
   {
      user = rssDetail.InnerText;
   }
   else
   {
      user = "";
   }
}
Run Code Online (Sandbox Code Playgroud)

我知道我需要定义命名空间,但我不确定如何做到这一点.帮助将不胜感激.

c# xml xmldocument xml-namespaces

11
推荐指数
1
解决办法
3万
查看次数

为什么XmlDocument.LoadXml抛出System.Net.WebException?

为什么System.Xml.XmlDocument.LoadXml方法抛出System.Net.WebException

这真是令人难以置信的疯狂,如果MSDN是对的,LoadXml最多应该给我一个System.Xml.XmlException.

然而,我有奇怪的例外,如:

底层连接已关闭:连接意外关闭.

Dim document As New XmlDocument
document.LoadXml("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><x></x>")
MsgBox(document.LastChild.Name)
Run Code Online (Sandbox Code Playgroud)

究竟是什么造成了例外?

.net xml vb.net xmldocument exception-handling

11
推荐指数
2
解决办法
3610
查看次数

名称不能以"1"字符开头,十六进制值0x31.第2行,第2位

在C#应用程序中加载XML文件时,我得到了

名称不能以"1"字符开头,十六进制值0x31.第2行,第2位.

XML标记就像这样开始.

<version="1.0" encoding="us-ascii" standalone="yes" />
<1212041205115912>
Run Code Online (Sandbox Code Playgroud)

我不应该不惜一切代价改变这个标签.

我该如何解决这个问题?

c# xml xmldocument

11
推荐指数
2
解决办法
2万
查看次数

如何以编程方式修改app.config中的assemblyBinding?

我试图通过使用XmlDocument类并直接修改值来在安装时更改bindingRedirect元素.这是我的app.config看起来像:

<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">            
            ...
        </sectionGroup>      
    </configSections>
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="MyDll" publicKeyToken="31bfe856bd364e35"/>
          <bindingRedirect oldVersion="0.7" newVersion="1.0"/>
        </dependentAssembly>
     </assemblyBinding>
    </runtime>    
...
</configuration>
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用以下代码将1.0更改为2.0

private void SetRuntimeBinding(string path, string value)
{
    XmlDocument xml = new XmlDocument();

    xml.Load(Path.Combine(path, "MyApp.exe.config"));
    XmlNode root = xml.DocumentElement;

    if (root == null)
    {
        return;
    }

    XmlNode node = root.SelectSingleNode("/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect/@newVersion");

    if (node == null)
    {
        throw (new Exception("not found"));
    }

    node.Value = value;

    xml.Save(Path.Combine(path, "MyApp.exe.config"));
}
Run Code Online (Sandbox Code Playgroud)

但是,它会引发"未找到"的异常.如果我将路径备份到/ configuration/runtime它就可以了.但是,一旦我添加了assemblyBinding,它就找不到该节点.可能这与xmlns有关吗?知道怎么修改这个吗?ConfigurationManager也无权访问此部分.

c# xml configuration xmldocument configuration-files

10
推荐指数
2
解决办法
1万
查看次数