标签: xmldocument

禁用对XML的实体引用

我目前正在研究必须从XML流中读取数据的程序,有时可能包含"&"符号

即:

<XMLRoot>
    <Element>
        <Node>
            This is a dummy data&more!
        </Node>
    </Element>
</XMLRoot>
Run Code Online (Sandbox Code Playgroud)

当我解析这个文本时,我收到一条错误消息,告诉我'对未声明实体的引用'.

有没有办法用C#的XMLParser删除"实体检查"?

.net c# xml xmldocument

0
推荐指数
1
解决办法
271
查看次数

为XmlDocument设置InnerXml会导​​致缺少End-Tag

        XmlDocument doc = new XmlDocument();
        doc.AppendChild(doc.CreateElement("Foo"));
        doc.DocumentElement.InnerXml = "Test";      
        StringBuilder result = new StringBuilder();
        doc.WriteContentTo(XmlWriter.Create(result));
Run Code Online (Sandbox Code Playgroud)

最后,结果是:

<Foo>Test
Run Code Online (Sandbox Code Playgroud)

这意味着缺少最终元素.为什么这样,我该如何解决?

.net c# xml innerxml xmldocument

0
推荐指数
1
解决办法
1555
查看次数

XPathSelectElements返回null

加载函数已在xmlData类中定义

public class XmlData
{
    public void Load(XElement xDoc)
    {
        var id = xDoc.XPathSelectElements("//ID");
        var listIds = xDoc.XPathSelectElements("/Lists//List/ListIDS/ListIDS");
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是从我的角度调用Load函数.

            XmlData aXmlData = new XmlData();

            string input, stringXML = "";
            TextReader aTextReader = new StreamReader("D:\\test.xml");
            while ((input = aTextReader.ReadLine()) != null)
            {
                stringXML += input;
            }
            XElement Content = XElement.Parse(stringXML);
            aXmlData.Load(Content);
Run Code Online (Sandbox Code Playgroud)

在加载函数中,即时获取id和listIds为null.

我的test.xml包含

<SEARCH>
  <ID>11242</ID>
  <Lists>
    <List CURRENT="true" AGGREGATEDCHANGED="false">
      <ListIDS>
        <ListID>100567</ListID>
        <ListID>100564</ListID>
        <ListID>100025</ListID>
        <ListID>2</ListID>
        <ListID>1</ListID>
      </ListIDS>
    </List>
  </Lists>
</SEARCH>
Run Code Online (Sandbox Code Playgroud)

c# xml xpath xmldocument

0
推荐指数
1
解决办法
6755
查看次数

Windows中的长路径错误?

我有以下文件:

C:\Users\Jan\Documents\Visual Studio 2010\Projects\AzureTests\Build\82df3c44-0482-47a7-a5d8-9b39a79cf359.cskpg\WebRole1_778722b2-eb95-476d-af6a-917f269a0814.cssx\39e5cb39-cd18-4e1a-9c25-72bd1ad41b49.csman
Run Code Online (Sandbox Code Playgroud)

我可以通过notepad ++中的打开窗口或通过资源管理器打开此文件.但是,通过"运行"窗口打开不起作用.它给出了"找不到文件"对话框.当我用C#查询文件系统时:

var dir = new DirectoryInfo(@"C:\Users\Jan\...")
var fil = dir.GetFiles("*.csman")[0];
Run Code Online (Sandbox Code Playgroud)

该文件也在返回的文件列表中,但我不能这样做:

var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(fil.FullName);
Run Code Online (Sandbox Code Playgroud)

因为这会导致'(1,1)'错误的数据不正确.因为XmlDocument认为文件是空的.但是File.ReadAllBytes这个文件上的成功.这有效:

var buf = File.ReadAllBytes(fil.FullName);
using (var ms = new MemoryStream())
{
    ms.Write(buf, 0, (int) buf.Length);
    ms.Seek(0, SeekOrigin.Begin);
    xmlDoc.Load(ms);
}
Run Code Online (Sandbox Code Playgroud)

调用时不会出现此问题...

xmlDoc.Save(fil.FullName);
Run Code Online (Sandbox Code Playgroud)

有人能解释一下这里发生了什么吗?

c# windows xmldocument

0
推荐指数
1
解决办法
247
查看次数

将Xml文档转换为C#Dictionary

我向服务发出请求并收到xml响应,如下所示.但是,我正在尝试将响应值存储在Dictionary中(或存储变量中返回的值),而我似乎无法使其工作.

任何帮助将不胜感激.

收到的xml回复:

<?xml version="1.0"?>
<ncresponse NCERRORPLUS="!" BRAND="ABC" PM="CC" currency="GBP" amount="10" STATUS="9" ACCEPTANCE="test123" NCERROR="0" NCSTATUS="0" PAYID="39409494" orderID="92E5CE91">
</ncresponse>
Run Code Online (Sandbox Code Playgroud)

c#代码:

        try
        {
            // Write data
            using (Stream postStream = request.GetRequestStream())
            {
                postStream.Write(byteData, 0, byteData.Length);
            }

            // Get response
            Dictionary<string, string> respValues = new Dictionary<string, string>();
            try
            {
                string body = String.Empty;

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    // Get the response stream
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("iso-8859-1"));
                    body += reader.ReadToEnd();
                }

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(body);

                XmlNodeList …
Run Code Online (Sandbox Code Playgroud)

c# xml xmldocument dictionary httpwebresponse

0
推荐指数
1
解决办法
2732
查看次数

如何在C#中获取按属性名称过滤的XML节点的值?

这是我的 XML 文件的一部分

<summary>
  <testcase>  
    <result value="-45">100</result>
    <result value="0">200</result>
    <result value="45">300</result>
  </testcase>
  <testcase>    
    <result value="-45">1000</result>
    <result value="0">2000</result>
    <result value="45">3000</result>
  </testcase>  
 <testcase>    
    <result value="-45">0.1</result>
    <result value="0">0.2</result>
    <result value="45">0.3</result>
  </testcase>
</summary>
Run Code Online (Sandbox Code Playgroud)

我需要通过按属性名称过滤来获取元素值。

作为示例,我需要获取属性 = 45 的所有值,然后答案是 300,3000,0.3

XmlDocument doc = new XmlDocument();
doc.Load(_xmlFilePath);
XmlNodeList nodelist = doc.SelectNodes("//testcase");

for (int i = 0; i < nodelist.Count; i++)
{
    Double value;                        
    Double.TryParse(nodelist[i].SelectSingleNode("result").Attributes["45"].InnerText, out value);
    Console.WriteLine("value : " + value);
}
Run Code Online (Sandbox Code Playgroud)

但上面的代码给出了以下错误消息。

你调用的对象是空的。

任何建议表示赞赏。

谢谢。

c# xml xmldocument xmlnode xml-attribute

0
推荐指数
1
解决办法
6304
查看次数

如何在 C# 中读取 XML 文档并将输出显示为字符串

考虑我的源文件看起来像这样。

        <Content xmlns="uuid:4522eb85-0a47-45f9-8e2b-1x82c78xx920">
            <first>Hello World.This is Fisrt field</first>
            <second>Hello World.This is second field</second>
   </Content>
Run Code Online (Sandbox Code Playgroud)

我想编写一段代码,从某个位置读取此 xml 文档并将其显示为字符串。

  say name of the xml file is helloworld.xml.
  Location: D:\abcd\cdef\all\helloworld.xml.
Run Code Online (Sandbox Code Playgroud)

我已经尝试过以下操作,但我无法做到。

            XmlDocument contentxml = new XmlDocument();
            contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml");
            Response.Write("<BR>" + contentxml.ToString());
Run Code Online (Sandbox Code Playgroud)

Response.write 不显示任何内容。如果我错过了什么,请纠正我。它没有创建任何组件并且错误即将到来。

我也尝试过这个,

            XmlDocument contentxml = new XmlDocument();
            try
            {
                 contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml");
             }
            catch (XmlException exp)
            {
                Console.WriteLine(exp.Message);
            }
            StringWriter sw = new StringWriter();
            XmlTextWriter xw = new XmlTextWriter(sw);
            contentxml.WriteTo(xw);
            Response.Write("<BR>" + sw.ToString());
Run Code Online (Sandbox Code Playgroud)

但我没有找到任何输出。

我想从某个位置读取 XML 文件并将其显示为字符串。

任何人都可以帮忙解决这个问题吗?

谢谢你,穆齐米尔。

c# asp.net xmldocument xmlreader

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

尝试更改XML节点但没有成功

我正在尝试model从以下.xml文件更改属性的值.

<MTConnectDevices xmlns="urn:mtconnect.org:MTConnectDevices:1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mtconnect.org:MTConnectDevices:1.1 http://www.mtconnect.org/schemas/MTConnectDevices_1.1.xsd">
  <Header version="1.1" sender="Company MTConnect Instance" creationTime="2010-08-26T19:00:47-07:00" instanceId="2" bufferSize="131072" />
  <Devices>
    <Device name="XX" iso841Class="1" uuid="000" id="id1001">
      <Description manufacturer="name" serialNumber="Serial Number" model="UNDEFINED">
      </Description>
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的,但它不起作用,因为它没有改变它的价值model.

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(mypath);
        var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
        nsmgr.AddNamespace("ns", xmlDoc.DocumentElement.NamespaceURI);
        var node = xmlDoc.SelectSingleNode("/ns:MTConnectDevices/ns:Header/ns:Devices/ns:Device/ns:Description", nsmgr);
        node.Attributes["model"].Value = "changed";
        xmlDoc.Save(mypath);
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我一把,告诉我如何解决这个问题?

.net c# xml xmldocument

-1
推荐指数
1
解决办法
88
查看次数

使用C#linq解析XML节点

我有像这样的xml文档:

<?xml version="1.0" encoding="utf-8" ?> 
<demographics>     
    <country id="1" value="USA">         
        <state id ="1" value="California">             
             <city>Long Beach</city>             
             <city>Los Angeles</city>             
             <city>San Diego</city>         
        </state>         
        <state id ="2" value="Arizona">             
             <city>Tucson</city>             
             <city>Phoenix</city>             
             <city>Tempe</city>         
        </state>     
   </country>     
   <country id="2" value="Mexico">         
      <state id ="1" value="Baja California">             
         <city>Tijuana</city>             
         <city>Rosarito</city>                     
      </state>     
   </country> 
 </demographics> 
Run Code Online (Sandbox Code Playgroud)

如何使用XML linq查询从人口统计信息节点开始选择所有内容,如下所示:

var node=from c in xmldocument.Descendants("demographics") ??
Run Code Online (Sandbox Code Playgroud)

c# xmldocument

-2
推荐指数
1
解决办法
1683
查看次数